Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 450
manash Avatar asked Aug 21 '12 12:08

manash


People also ask

How long do WebSocket connections last?

However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.

Are WebSocket connections persistent?

A WebSocket is a persistent connection between a client and server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection.

Why WebSockets are not reliable?

There is no guarantee of the successful delivery of a web socket message to the peer, but if the action of sending a message causes an error known to the container, the API throws it.


2 Answers

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

like image 146
kanaka Avatar answered Sep 22 '22 18:09

kanaka


Long-lived connections is what WebSocket was designed for. Depending on how your clients connect, those connections might nevertheless be limited in lifetime, i.e. on retail DSL connections, there often is a forced reconnect every 24h at least.

Then, what you seem to desire is something like publish and subscribe messaging pattern on top of raw WebSocket (which only provides bidirectional messaging). Have a look at: http://wamp.ws (and http://autobahn.ws).

Disclaimer: I am original author of WAMP and Autobahn and work for Tavendo.

like image 40
oberstet Avatar answered Sep 22 '22 18:09

oberstet