[Pharo-users] Problem with ZnWebSocket and closed connections

Sven Van Caekenberghe sven at stfx.eu
Mon Feb 24 07:41:49 EST 2020

Hi Davide,

First I'll explain why you see a difference in behaviour, then I'll give you a solution.

Network connections are expensive resources, most servers try to close them as soon as possible when they are not used.

Zinc HTTP Components does not normally close connections by itself, although timeouts do naturally occur.

The WebSocket code normally loops on the server side when timeouts occur while waiting for incoming messages (more specifically in ZnWebSocket>>#runWith:). Note that even though it loops, there is no actual communication.

Both previous facts, not closing unused connections and looping on timeouts, result in a working echo/chat example, when run locally.

When you put a proxy in front of this, the proxy (like nginx) might decide to close an unused connection. This is allowed and it is what you are seeing. This is not an error and thus normal.

Of course, you might want to keep such connection open for longer, even if they are not used. This can be done by keeping them alive. Note however that this might result in many open connections.

How do you keep a connection alive ?

One solution is to do this at the application level. ZnWebSocketStatusHandler does this by sending a message every second.

The WebSocket protocol (https://tools.ietf.org/html/rfc6455) does define a number of control messages, all which were already implemented as such. Two of these messages, ping & pong, are meant to be used for implementing keep alive. Client side, this was already done (ZnWebSocket>>#handleControlFrame:).

I now added automatic keep alive message sending server side as well.

This can be seen in the updated implementation of ZnWebSocket>>#runWith: 


Now, whenever the read times out (as defined by the socket stream's timeout, a Zn setting), a ping control packet is sent to keep the connection alive, the client will answer with a pong, and then the server loops.

This should be sufficient to solve your issue (but both the proxy's as well as the Zn timeout need to be compatible, Zn's needs to be smaller).

Please let me know if this works for you.



> On 23 Feb 2020, at 18:19, Davide Varvello via Pharo-users <pharo-users at lists.pharo.org> wrote:
> From: Davide Varvello <varvello at yahoo.com>
> Subject: Re: Problem with ZnWebSocket and closed connections
> Date: 23 February 2020 at 18:19:02 GMT+1
> To: pharo-users at lists.pharo.org
> Hi,
> I put nginx as a websocket proxy and I run the ZnWebSocketEchoHandler.
> Unfortunately also with this configuration the connection closes it after a
> couple of minutes of inactivity
> Cheers
> Davide
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

More information about the Pharo-users mailing list