Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between websockets and long polling for turn based game server

I am writing a server for an iOS game. The game is turn-based and the only time the server needs to push information to the client is to notify of the opponent's move.

I am curious if anyone could comment on the performance and ease of implementation differences between using WebSockets and long polling. Also, if I used WebSockets, should I only use it to receive information and send POST requests for everything else, or should all communication be through the WebSocket?

Additionally, is there anything extra to consider between WebSockets and long polling if I am interested in also making a web client?

like image 960
acidic Avatar asked Jul 30 '15 04:07

acidic


People also ask

What is the difference between WebSocket and long polling?

WebSockets are Full-Duplex meaning both the client and the server can send and receive messages across the channel. Long Polling is Half-Duplex meaning that a new request-response cycle is required each time the client wants to communicate something to the server.

What are the differences between long polling WebSockets and server sent events?

Long-polling opens an HTTP request and remains open until an update is received. Upon receiving an update, a new request is immediately opened awaiting the next update. Server-sent events(SSE) rely on a long-lived HTTP connection, where updates are continuously sent to the client.

Should I use WebSockets or SSE?

SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill. It's always wise to use the best tool for the job.

What are the disadvantages of WebSockets?

The biggest downside to using WebSocket is the weight of the protocol and the hardware requirements that it brings with it. WebSocket requires a TCP implementation, which may or may not be a problem, but it also requires an HTTP implementation for the initial connection setup.


2 Answers

For anyone else who may be wondering, it could depends on how long typical interactions go between events?

Websocket: Anything more than a few tens of seconds, I don't think keeping a websocket open is particularly efficient (not to mention that IIRC it would disconnect anyway if the app loses focus)

Long polling: This forces a trade-off between server load (anything new now? how about now? ...) and speediness of knowing a change has occurred.

Push notifications: While this may be technically more complex to implement, it really would be the best solution IMO, since:

  • the notification can be sent (and delivered) almost immediately after an event occurs
  • there is no standby server load (either from open websockets, or "how about now?" queries) - which is especially important as your use-base grows
  • you can override what happens if a notification comes in while the user is in-app
like image 138
Adam Smooch Avatar answered Sep 25 '22 14:09

Adam Smooch


should I only use it to receive information and send POST requests for everything else

Yes, you should use WebSockets to fetch real-time updates only, and REST APIs to do BREAD stuff.

should all communication be through the WebSocket?

Short answer: No,

Check this article from PieSocket for more information about the best use cases for WebSockets. What Is WebSocket: Introduction And Usage

like image 33
Anand Singh Avatar answered Sep 24 '22 14:09

Anand Singh