javawebsocketnetty

Are Websockets adapted to very long-lived connections?


I'm thinking about using web sockets with Netty for an application where clients connect to a server to get some information at first. Then, they are registered by the server and any changes on the information of a particular client will trigger a notification to the client containing the updated information. In this case, the communication is first initiated by the client and is latter initiated by the server. So, web sockets seem to be adapted for this situation.

But, after it is up, I want my client to be able to be notified by the server at any time. It may be one day after as weeks after. So, my question is are very long-lived connections possible using web sockets?

Thanks


Solution

  • Absolutely, but there are a couple of caveats.

    If you want your connection to stay alive continuously for long periods then I would suggest adding some logic to your client to reconnect when the onclose event happens (you will want some sort of back-off to prevent a tight reconnect loop for certain situations).

    You may also want to send a ping message (a simple message that is ignored) every 5 minutes or so to prevent idle timeouts (which can happen at several places along the network connection). TCP network stacks are often set to kill connections that have been idle for 2 hours and ping messages will keep them alive. Browsers are allowed to implement ping messages that are invisible to the application but this is optional so you should implement your own at the application level if you want to guarantee this behavior.

    Note: Ping/Pong frames are part of the WebSocket spec but as yet are not available via the API