Problem with ZnWebSocket and closed connections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with ZnWebSocket and closed connections

Pharo Smalltalk Users mailing list
Hi guys!
I've a problem with websockets, I'm quite a noob on the subject and I'm
doing some tests with them.

I run the echo websocket this way:

ZnServer startDefaultOn: 8000.
ZnServer default delegate: (ZnWebSocketDelegate handler:
ZnWebSocketEchoHandler new).


I tested it with with the chrome extension
(https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo)
that emulates a websocket client. If I point to the url ws://localhost:8000
everyting is fine
BUT
I want to test the websocket server via ngrok (https://ngrok.com/) and if I
point the client to the ngrok url (i.e. ws://myurl.ngrok.io that maps
localhost:8000) it happens a connection-closed after a while and I can't
understand the cause, is there a timeout somewhere?

I know that it is something related to Pharo (and not to ngrok) because if I
implement a simple echo websocket served via websocketd
(http://websocketd.com) the connection stays always active even if through
ngrok.

Can you help me please?
TIA
Davide




--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Sven Van Caekenberghe-2
Hi Davide,

I am away until next week, then I might try to answer, though you are talking about a complicated setup.

Sven

> On 20 Feb 2020, at 19:08, Davide Varvello via Pharo-users <[hidden email]> wrote:
>
>
> From: Davide Varvello <[hidden email]>
> Subject: Problem with ZnWebSocket and closed connections
> Date: 20 February 2020 at 19:08:43 GMT+1
> To: [hidden email]
>
>
> Hi guys!
> I've a problem with websockets, I'm quite a noob on the subject and I'm
> doing some tests with them.
>
> I run the echo websocket this way:
>
> ZnServer startDefaultOn: 8000.
> ZnServer default delegate: (ZnWebSocketDelegate handler:
> ZnWebSocketEchoHandler new).
>
>
> I tested it with with the chrome extension
> (https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo)
> that emulates a websocket client. If I point to the url ws://localhost:8000
> everyting is fine
> BUT
> I want to test the websocket server via ngrok (https://ngrok.com/) and if I
> point the client to the ngrok url (i.e. ws://myurl.ngrok.io that maps
> localhost:8000) it happens a connection-closed after a while and I can't
> understand the cause, is there a timeout somewhere?
>
> I know that it is something related to Pharo (and not to ngrok) because if I
> implement a simple echo websocket served via websocketd
> (http://websocketd.com) the connection stays always active even if through
> ngrok.
>
> Can you help me please?
> TIA
> Davide
>
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Pharo Smalltalk Users mailing list
Hi Sven,
Sorry, I put too many useless details in my previous email.

The core issue is that if I point to the websocket directly
(ws://localhost:8000) the connection lasts, otherwise it disconnects after a
while.

Cheers
Davide



Sven Van Caekenberghe-2 wrote
> Hi Davide,
>
> I am away until next week, then I might try to answer, though you are
> talking about a complicated setup.
>
> Sven
>
>> On 20 Feb 2020, at 19:08, Davide Varvello via Pharo-users &lt;

> pharo-users@.pharo

> &gt; wrote:
>>
>>
>> From: Davide Varvello &lt;

> varvello@

> &gt;
>> Subject: Problem with ZnWebSocket and closed connections
>> Date: 20 February 2020 at 19:08:43 GMT+1
>> To:

> pharo-users@.pharo

>>
>>
>> Hi guys!
>> I've a problem with websockets, I'm quite a noob on the subject and I'm
>> doing some tests with them.
>>
>> I run the echo websocket this way:
>>
>> ZnServer startDefaultOn: 8000.
>> ZnServer default delegate: (ZnWebSocketDelegate handler:
>> ZnWebSocketEchoHandler new).
>>
>>
>> I tested it with with the chrome extension
>> (https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo)
>> that emulates a websocket client. If I point to the url
>> ws://localhost:8000
>> everyting is fine
>> BUT
>> I want to test the websocket server via ngrok (https://ngrok.com/) and if
>> I
>> point the client to the ngrok url (i.e. ws://myurl.ngrok.io that maps
>> localhost:8000) it happens a connection-closed after a while and I can't
>> understand the cause, is there a timeout somewhere?
>>
>> I know that it is something related to Pharo (and not to ngrok) because
>> if I
>> implement a simple echo websocket served via websocketd
>> (http://websocketd.com) the connection stays always active even if
>> through
>> ngrok.
>>
>> Can you help me please?
>> TIA
>> Davide
>>
>>
>>
>>
>> --
>> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>>
>>
>>





--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Pharo Smalltalk Users mailing list
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

Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Sven Van Caekenberghe-2
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:

https://github.com/svenvc/zinc/commit/9073acc2fad197c2787c6cd0f4d315981ebb98b5

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.

Regards,

Sven

> On 23 Feb 2020, at 18:19, Davide Varvello via Pharo-users <[hidden email]> wrote:
>
>
> From: Davide Varvello <[hidden email]>
> Subject: Re: Problem with ZnWebSocket and closed connections
> Date: 23 February 2020 at 18:19:02 GMT+1
> To: [hidden email]
>
>
> 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
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Pharo Smalltalk Users mailing list
Hi Sven,
Thanks for your reply.
I supposed that the ping/pong feature would have helped (I'm not an expert,
but I found something about it on the net).
Tomorrow I'm gonna try your updated runWith: and I'll feedback you.

Thanks
Davide


Sven Van Caekenberghe-2 wrote

> 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:
>
> https://github.com/svenvc/zinc/commit/9073acc2fad197c2787c6cd0f4d315981ebb98b5
>
> 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.
>
> Regards,
>
> Sven
>
>> On 23 Feb 2020, at 18:19, Davide Varvello via Pharo-users &lt;

> pharo-users@.pharo

> &gt; wrote:
>>
>>
>> From: Davide Varvello &lt;

> varvello@

> &gt;
>> Subject: Re: Problem with ZnWebSocket and closed connections
>> Date: 23 February 2020 at 18:19:02 GMT+1
>> To:

> pharo-users@.pharo

>>
>>
>> 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
>>
>>
>>





--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Problem with ZnWebSocket and closed connections

Pharo Smalltalk Users mailing list
In reply to this post by Sven Van Caekenberghe-2
Hi Sven,
It's working! Thank you very much
Davide



Sven Van Caekenberghe-2 wrote

> 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:
>
> https://github.com/svenvc/zinc/commit/9073acc2fad197c2787c6cd0f4d315981ebb98b5
>
> 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.
>
> Regards,
>
> Sven
>
>> On 23 Feb 2020, at 18:19, Davide Varvello via Pharo-users &lt;

> pharo-users@.pharo

> &gt; wrote:
>>
>>
>> From: Davide Varvello &lt;

> varvello@

> &gt;
>> Subject: Re: Problem with ZnWebSocket and closed connections
>> Date: 23 February 2020 at 18:19:02 GMT+1
>> To:

> pharo-users@.pharo

>>
>>
>> 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
>>
>>
>>





--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html