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 to extra to consider between websockets and long polling if I am interested in also making a web client?
What is Long polling ?
A variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll.
Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event.
In a web/AJAX context, long polling is also known as Comet programming.
What about Websockets ?
WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time.
Conclusion :
If there is a need of Real time communication you can very well opt for websockets .
But in Long Polling :
A connection is held open between the web client and the web server so that when the server has new information it can push it to the client. That request is then finished. A new request is then made between the client and the server and then wait for another update from the server. The same TCP connection is generally open persistently throughout multiple requests due to HTTP/1.1 keep-alives.
References and other considerations :
PubNub long polling vs sockets - mobile battery life
What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?