P2P (peers do not connect outside of the local network) using peerjs broker server

For me everything is working fine… again…

Could you do 2 things to investigate the problem:

  1. Use external (non gdevelop) mosquitto publishers and listeners to post and listen for messages, to see if the problem is coming from receiving messages or publishing them
  2. Look at the javascript console for errors. If something fails to publish, the library should probably log the reason there. You can opßen it by pressing Ctrl-Shift-I on the preview window.
  1. works. I did that already.

  2. I see that at the moment I create the room the program is flooding the network like a waterfall with broker server connection requests I guess. Even though the connection to the broker server is at the beginning of scene only it keeps repeating this infinitely it seems after I create room from the lobby.

EDIT: Looks like it happens when I change scene. I see that a message published to a topic before I change to another scene is not sent. But it starts looping somewhere.

I took out the change scene and only use variable to sent and it starts looping too as soon as I press the button Create Room which call for a subscription to the topic of GlobalVariableString(PlayerName) and then sent a message on that channel with the same variable.
(Subscripe to topic GlobalVariableString(PlayerName) with QoS at 2.
Publish variable Global VariableString(PlayerName) on topic GlobalVariableString(PlayerName) (QoS:2)

Can you elaborate? Did the messages get sent? Did the messages sent by GDevelop get received by the broker? Do the messages get forwarded to GDevelop?

:thinking: Maybe MQTT doesn’t like that you resubscribe to the topics every frame. Try to add some trigger onces.

The messages and subscriptions done to fixed subscriptions (i.e. “Lobby” and “Room”) do work fine. I can receive and sent messages between the clients and act accordingly. The messages are received by the broker and gdevelop thus also receives them from the broker.

It is when I subscribe to a topic that is a variable (be it global or scene) that this happens. Trigger once and also using a variable condition alike my example below will both result in this looping behavior too somehow.

I really would like to be able to subscribe to a topic that is stored withing a variable to make sure all games have their own unique identifying topic.

I want it not to be done every frame anyways else there is to much unwanted communication on the network.

From what I tested locally, the request seems to be made only when connecting to the broker. Make sure that you connect only once in the game runtime to the broker

Did that already I now even subscribed to it at the same time as I subscribe to the hard coded topics. The problem occurs immediately, (see below)

Triggering once or creating a variable exception to make sure it only will occur once make no difference.

Btw thank you for the tip to be able to look under the hood for I am now also reviewing my P2P code like this, it is very helpful)

I checked again, and even going offline and online again doesn’t break this connection. The only way that the network tab shows more than one connection is that you connect to the roker server multiple times.

I connect to the broker when starting the game by having this in an initialisation scene, and never going on that scene again:

(Don’t worry about the console commands, it’s my private little debugging extension)


And the result:
image
Only one connection, and everything is working without problems. As you see I also used your server so there shouldn’t be any interference due to using a different server.

See how 2 connections = 2 requests:


image

I went slightly mad but I have discovered a culprit. In creating a player name I use the text-input object. I have a condition with if key pressed enter and a condition with a click on a button that make it go to the next scene. In testing your code above (indeed it works fine) I started to investigate what it is in mine that make the behavior erratic. It is the Enter in the variable. Once I have this enter key in the variable of the PlayerName the looping starts. When I do not press enter in the key input and just click on the button connect then the connections work normal.

So the input needs an inverse condition of the return key (see below) making sure it does not slip into the variable. Now all is working normally. Thank you for your help.

I wonder what it might have affected before. With this situation I am needing to make random keys myself now to make sure double player names do not open the same topic which in P2P was nicely solved by the broker server.

I still mourn that I can not work around those NAT situations with the TURN and STUN server but one day I might figure it out. for now the game works with p2p and will make a version with MQTT now. Thanks for your great help.

1 Like

Game looks great! I am making a board game currently for two players in local multiplayer mode, that is both players on the same device. I have started working on the online version of it and my main concern is how to handle network errors, e.g. lost packets. Do you have any plan on how to handle lost packets from either the clients or the server?

If you use MQTT, the QoS setting let you tell the library to handle that. If you have a QoS of 2, there will be a double handshake to make sure everything is received correctly.

1 Like

In the p2p version I handle it per turn. Every client has to confirm that it received the last placed stone before the next player is initiated. Because my game involves three steps every turn it could go wrong half way in the turn. I can program a check per move later but it seems a bit overkill. In the MQTT situation it is indeed handled by the quality of service that you request. no 2 being the highest ( a four way handshake). But the big disadvantage I feel is that all game data will be running over the server network (which needs to be paid for and I am not google). That is why P2P would be the way to go because then only the initial broker deal is done over the network. Unfortunately with the human nature of having secrets, wanting privacy and not enough addresses to go around it is sometimes hard it seems to penetrate through NAT and firewalls. Also I have discovered some antivirus software will block the traffic done through P2P. So much for good intentions :D. I am now finishing up the P2P version first so that it will skip the players that are disconnected from the game and then put it online to stress test it in the world for those who want to play it and see what happens. After that I will probably make an MQTT version. The trouble of MQTT is not the delivery but to see if someone is still there (disconnect from server). But all in good time. Just do not get the return/enter code in your variables like me or you’ll be pulling you hairs out of your skull wondering why it is not working :smiley:

Yes, I see what you mean. Acknowledging a received packet seems like a good way to make sure server-sent data has reached all the clients.

P2P has quite a few dependencies and requirements in order to work properly. Given the number of different NAT topologies chances are that true-P2P will be pretty limited. You will end up having the data passing through a proxy (TURN) server. Given the small size of the data packets the traffic shouldn’t be that much though. MQTT looks like a very good approach for bypassing NAT traversal issues, but it seems you have zero control on the server logic. You just keep creating ‘rooms’ for every game which is left on the server when the game is finished. Not hard to by-pass or control though, with the help of a simple REST API that will handle the room ID creation and book-keeping.

In my case, I actually looked at SSE (Server-Sent Events) which is a simple stream-based unidirectional connection to a web server. The protocol supports automatic re-connection and Last-Id-Received header for the clients, in order to keep track of the last packet received by the client. You still need to use HTTP POST to update the server with your move. Finally, you have to code the server by your self, which also gives you full control on what you can do. Still, you don’t get an easy way to handle errors or lost packets.

I just finished rewriting the game to use a Mosquitto server and MQTT as you tipped. I have a fully working version on http://144.91.116.148/ElementsOnlineWeb/index.html now running. I am now getting some minor bugs out but four player can play the game together now. I want to thank you Arthuro555 for all your help and interest in helping me out. It has been very helpful.

I hope some people will be playing it and give feedback (bug reports and idea’s how the game might improve).

1 Like

I still want to look into this one issue regarding P2P (I know it is experimental). But working with MQTT, P2P and Google FireBase the last few weeks I come to the conclusion that for making a multiplayer game, P2P in combination with FireBase would be ideal. P2P has a server/client disconnect function and is pure p2p which would cost far less data traffic when it comes to the kind of games I would like to develop n the future. BUT…

With the samsung galaxy 8 and newer models of phones I cannot seem to connect to the peerjs server. When this thread started I had not secured it with SSL and I was guided to MQTT. That gave me the same problem until I secured it with SSL websocket and the Samsung galaxy S8 was connecting to the MQTT server correctly. I thus thought that if I would maybe also do it with the peerjs server like that and it would maybe solve the old initial problem of this thread. I have started the peerjs server with SSL and can connect with all devices except with the newer phone models like Samsung S8 and higher.

Connecting to the broker server with SSL enabled and trying to extract P2P::GetID() in gdevelop does not work on newer phone models. It is really sad :frowning: … Because It would be the best way to have a multiplayer work. Does anyone have this problem too or knows what is going wrong. I have tried it on 3 newer devices and they all refuse to work while on web and older mobile phones it works like a charm.

Else I am stuck having to use the MQTT server again for this new game too.

I know it is a long shot but who know maybe some of you know what I am doing wrong.

I added that to MQTT a few days ago :eyes:

Sadly, it is hard to tell without being able to replicate the issue :confused: . I don’t have a server with SSL nor a phone with this issue, so I can’t really look into it.

WHAT !!! WOW. Will that be available with the next update?

Yes I understand that. It goes to deep for me. I believe it must have to do with the fact it is in a cordova browser or something like that and that the app is only allowed pure http requests or something like that. And the phone does not allow the app to do anything else. Any way far above my head and pay grade unfortunately.

It’s already there. Redownload the extension from the store to get the updated version. Careful though, there is a breaking change as I removed the Settings_* expressions in favor of using JSON, if you were using them you will need to change it.

Also unsubscribe topic I see (*Happytears). Very Nice. Thank you.

1 Like