We are developing a mobile game for iOS/Android, and we have a stateless WebAPI receiving requests from the game and saving stuff to the DB. Basically we keep sensitive information on the server side (currencies, items owned, etc etc) and the client sends as little information to the server as possible, to avoid cheating and so on. We have this structure working properly and the game is coming along nicely, however I would like your opinion on what would be the best way to make a server->client communication instead of client->server… for example, a chat where one user sends the message to the server, and the server broadcasts instantly to the users in that chat group (such as a guild chat). Think of the features in Clash Royale such as guild notifications, card donations… this kind of stuff.
Is there an “ideal technique” that you guys use on your own games? Thanks!
PS: We barely have any multiplayer features… we need this for a chat, for friend requests, and this kind of stuff only, at least for now. We are not sure how we can make the server communicate with the client, without the client starting the communication first.
In the client/server model, the client is always the one who initiate the communication. Though you could have the devices of your players acting as servers, but I don’t see why that would be necessary.
Why is it a problem that the client starts the communications?
The thing is, there are things which the client doesn’t know so he is unable to start the communication. For example, someone in my guild sends a chat message… another client in that guild doesn’t know that, how can he be notified without having to ask the server every second? Same for friend requests for example, isn’t there a way for the server to warn the client like “look, someone added you as a friend, here is his ID and stuff”?
Use a multiplayer Service, just like Photon Engine!
I do this in all my games! What you could do is : When A user types “Submit message”, you send an RPC Event(Function that is called to anyone you send) to others to tell them that something was sent and they need to check database. This is a very easy way and optimal way in my opinion
There are frameworks that facilitate this for you. As have been suggested.
If you want to set it up manually, what usually happens is that when the client connects to the server, it hands over some credentials about itself so the server knows who it is (like a player id). Well you’ll just also hand over callback data as well… what data you choose is dependent on the connection model.
For example, if it’s a socket connection, you’d just hand over the ipaddress and port that the callback will be sent across.
Then on your client you’ll keep your channel open waiting for messages, usually on a separate thread. In the case of a Socket, you just open up the socket on a thread, and it’ll block until something connects to the socket.
You can send messages through said channel and interpret them.
At the socket level, the client connects to a known server (ip + port) to established a connection (it’s always it that direction client → server). Once the connection is established it does not matter who is sending the first piece of data, it could be either the server or the client. How the communication exactly goes, depends on the protocol you are using, which can be a protocol you’ve defined yourself or a protocol implemented by some framework.