Seek opinion on a networking problem



Amin Mousavi
Hi all,

It is not AB relevant but more a generic network question. I am building a multiplayer puzzle game, using AB for its UI. To give a bit of context, each player can make a move every 15 seconds so it is not a real time networking matter. For the networking I have googled a lot and my findings and thoughts end up to the below options:

1- Use a websocket for communication
2- Clients call an API once every second and to learn about any updates by the other player
3- When a player makes a move calls an API that uses FCM to send a push notification with the data needed to the other and receive a confirmation notification in return

I did not come across the third option anywhere so it gets me thinking maybe it is not a good idea. Given the very low amount of data that goes and returns, many suggest option 2 (I expect a 10 letter string every 10 seconds or so to be sent to the other player, but I need a call every second to avoid delays). But it seems like option one is the fastest and easiest to scale.

Now what brought me here is lack of my knowledge on servers and specially websockets. Could a shared server serve 100 players live and all calling an API once a second? or I better off get a websocket from somewhere even though I do not really need a real time communication.

Any insight is very much appreciated

Samuel

Hello Amin,
Here are my 2cts. If by "shared server" you mean a VPS, it depends on the technical specifications of the server itself and the bandwidth provided.
Technically there is no reason not to be able to handle 100 concurrent connections using a Linux VPS with 4 GB of RAM, 4 GB of SWAP and 100 Mbps connection if dedicated to the app itself.

After that aspect, it depends on the amount of data to manage and of course any other operation you have to do on them. That aspect is related on your experience only.

About the solution, there is something you did not mention, it is MQTT (https://mqtt.org/) and by experience I would use it to send the data between the players and use a socket or pusher to inform the other parts in real time (disconnection, new movement, etc.) because that protocol has a multi subscribers possibility.

I hope it can help a bit. Good luck

David Esperalta

Hello to all,

Thanks for your post, Samuel. Honestly I am not an expert in the server side, however, I think Samuel is correct, that is, probably we are talking about the server's hardware capabilities at the end. Of course, other things must be taken in consideration.

For example, from your three options, I will choose the first one, if possible, discarding the second one, if there is another way. One of the apps that I am developing (also using AB for the client side) uses a websocket with very, very good results.

The app that I am talking about are daily used by more or less 500 people (workers of a company) and the websocket provide to the app basically a chat feature: a very powerfull chat, so we know when someone is online, is typing, etc.

We use the websocket's server also in combination with push notifications, so, if an user is not online (is not in the app), we send to it a push notification, who open the app, who recognize the push type and shown to the user the latest chat's messages.

Once we can go working with the websocket... we love it. I think there is nothing that can be better, except maybe your third option, that is, suposing we no need a websocket, a real bidirectional communication between the server and the clients, and viceversa.

The main problem for the websocket I think it's the required of an own server, that is, probably (but we can talk with our hosting provider) install a websocket server into a shared hosting, for example, like the hosting who serve this webpage.

We need to install a websocket server (we use a Java websocket, if anyone are interested, I can provide here more information), work with it (on the server side) and also in the client side, of course, with the WebSocket control of AB, for example.

Can take a while... I remember we expend several time and work to setup the websocket, etc., but finally it's one of the best feature of the app in my opinion. And that's what I can say at this time about this... not too much help (like Samuel provide), but go ahead and post here any further question or comment.



Amin Mousavi
Hi Samuel,

Thanks a million for taking the time and respond. I actually am using a shared hosting sorry for misprinting. Honestly so far I have only used shared servers on CPanel and Plesk and Virtual machines on windows and was not familiar with option of VPS. From what I gather with a fraction of cost of VM I can get a VPS and that is great! It just puzzles me how come an enterprise level shared hosting is this much more expensive in comparison to a VPS? (I came across OpenStack KVM, 1 vCore(s) From 2 GHz, 4 GB RAM, 40 GB SSD, Local RAID for 72 euro a year but this shared hosting costs 260 on Spiralhosting that is one of the cheap ones! 25GB SSD Diskspace, 500GB Monthly Bandwidth,1.5GB RAM, 50 Concurrent Connections). My plan was to upgrade the shared hosting plan when resources run low to avoid migration but it seems like I have to migrate at the end :)

I read about MQTT and found a PHP library for it. I should study more about it and weigh the pros and cons for my implementation.

Your insights were very very helpful, I greatly appreciate that. As I said when it comes to server hardware I am a newbie. My expertise is SQL, I hope I can return the favour there some day :) Will keep you posted here on how I proceed.

Amin Mousavi
Hi David,

Thanks as always for your great help. It is very much appreciated.

I also did not like the second option but because its implementation is very convenient, it was tempting and an evil voice in my head was saying "don't worry it will work, it can pull the weight, you don't need to redo every thing over again" :D well but I know it is not the best option myself.

We don't really need a real time bidirectional connection between the players because the pace of the game is very slow and the amount of data being sent and received is very small. The only part in option 3 that worries me is the error handling. The maximum number of players in one game is 4. So lets say one of them makes a move, the three other should receive a notification and if all deliver then the player who has made the move also should be notified. So what if one them don't deliver, or delivers 1-2 seconds late? it disrupts every thing, if not handled properly. It still is a raw idea but risks associated to it I think is very serious so it is likely that I discard that too.

I think I need to make the hard choice here, go with the harder but more reliable solutions. I study MQTT that Samuel suggested and Websockets a bit more, study more about VPS, and any other options that I might have in a budget price range, and then finalise the plan. I think so far that I am better off with getting a VPS and implement either MQTT or websocket on it for the communication and rest assured that I did it the right way.

David Esperalta

Hello to all,

I think in your last post you are talking about some possible scenarios of the app, which of course you may need to control. In other words, the app must be ready to work in different scenarios, like what you describe using questions: "So what if one them don't deliver, or delivers 1-2 seconds late?" I supose that things like that must be implemented in an app level, that is, it's a question of the app's implementation. Am not saying that this all are easy, of course... just that maybe it's a question of think about, work on it, etc. Step by step, question by question...

Yes; again my help try are not very useful at all... but is what I can say right now: supose a bad connection scenario... then your server hardware can't do nothing,... so in my opinion it's the app who must control that kind of possible scenarios... in order to maintain the things working... at least in the best way as possible.



Amin Mousavi
Hi David,

You are exactly right. Errors should be handled at the app level and has nothing to do with the network part. For plan 3 and implementing all the communication by notifications the down side in my opinion is that even though in principle it must reduce the number of times systems should communicate with each other but after considering the error handling and high probability of them occurring, number of times that systems should communicate with each other increase and it may defeat the whole point of the design. Any how, cross fingers I will start working on it on Monday if nothing else goes on fire in the mean time :D and will keep you posted here how I get it done.

Thanks a million for your assistance as always

David Esperalta

Hello Amin,

If with "notifications" you mean push notifications... note that they works like a charm, but, not always in real time... because the notification is finally sended by the thirdparty service that we use, like Google FCM. Or maybe I am a bit wrong and you mean other thing but not exactly push notifications... or there is not a problem to receive that notifications "not in real time" (even when sometimes... they arrive in a merelly real time).



Amin Mousavi
Hi David,

Thanks a lot for your great help as always!
Yes I meant push notifications, sorry for the confusion. We currently use FCM for one of our AB projects and started off with the PHP codes you provided on the samples and as you described they work like a charm hence the third option design. Yet the chance for the delays and the fact that it is a third party service made us to decide to implement it with the WebSocket and use push notifications for error handling. So David, if you don't mind me asking, you mentioned earlier that you have implemented the websocket with JAVA and can provide more details, I appreciate it so much if you can elaborate.

Thanks again for the great support

David Esperalta

Hello Amin,

First of all, please, sorry for the delay. Believe or not, I am working very hard in certain app these days and I have no time to write a reply! In fact that's the app in which we are using a websocket server for the app's chat and other features.

I have a problem, because, I am the responsible for the client side (working with AB), but not the server side. I ask yesterday to my boss (which is responsible for the server side, among other team members), about the websocket software that we are using: I really think that we use certain specific software, but, my boss told me we are not doing that.

When I ask about the software in the server he told me we use Java "glasfish", which is an "app server" similar than Java "TOMCAT". So it's "glasfish" who launch, execute or maintain the websocket server... WHICH HAS BEEN WRITTEN FROM THE SCRATCH... my boss offer to me this forum thread, in which apparently they base to prepare the websocket server.

And that's all what I can say... except if you want to ask something in particular: in this case I will try to find the right answer or directly reply if possible. No; not too much information, I know, Amin, but you must look for some possible "sample Java websocket server", follow the referred forum link, maybe it's possible to use some software which already offer to us what we need...

The client site (with AB) is more easy... requires their work, of course (I can remember when we develop the chat...), but what we must do is to use the WebSocket control, and, mainly, their Message event. Basically the server can send anything to the app... and viceversa, we commonly use JSON objects to transfer data, which appear very good, since a sole object can have several properties and values.

That's all for now, Amin... not too much help... but maybe we can continue ahead here or in other posts.



Amin Mousavi
Hi David,

Thanks a lot indeed for taking the time to respond to this question and following it up with your colleagues despite your busy schedule.

It was so much help, I will follow the links you recommended for the server side implementation.

All the best

David Esperalta

Hello Amin,

Thanks for your consideration. Certainly I don't provide too much help... maybe in the future. Maybe other mate know about a possible server websocket software to be used. I think that can be more or less easy to find information... following the above link... or just searching by something like "Java server websocket implementation"...

In fact it's rare that no one already exists... but I can't recommend one of them as I wanted... I am wrong... I figure out that we are using some specific software, but, no... we develop the websocket sever from the scratch. Maybe this can give us some information... probably it's not very, very difficult or complex to start with it... of course later the software can become more complex: database queries, etc., but, probably start with it (using Java, Glasfish, etc.) maybe it's not very, very complex at all...

What I mean is that probably I am missing something... for example, maybe a websocket server is in fact an HTTP server with some kind of particular configuration or something like that? That's what I start to think and what I want to say here...



Amin Mousavi
Hi David,

Sure thanks, doesn't look very complicated! I am considering https://github.com/bloatless/php-websocket at the moment but there are other options too.
Thanks a mill!

Everybody can read the DecSoft's support forum for learning purposes, however only DecSoft's customers can post new threads. Purchase one or more licenses of some DecSoft's products in order to give this and other benefits.