8000 Questions about maintaining an eternal connection · Issue #3301 · warmcat/libwebsockets · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Questions about maintaining an eternal connection #3301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
a412h opened this issue Dec 20, 2024 · 3 comments
Closed

Questions about maintaining an eternal connection #3301

a412h opened this issue Dec 20, 2024 · 3 comments

Comments

@a412h
Copy link
a412h commented Dec 20, 2024

This is not a bug report, the library is working perfectly.
I just would like to know exactly which parameters must be set in a client code to maintain a permanent connection to a server.
In particular, with the following example:
https://github.com/warmcat/libwebsockets/blob/main/minimal-examples-lowlevel/ws-client/minimal-ws-client-binance/main.c

  1. Let us suppose that the server requires a ping every 60 seconds to keep the connection alive,
    and that the client needs to have as few disconnection as possible (for example the client is receiving high frequency data, incrementally computes a variable from this data as it arrives, but the variable would be badly constructed if some data were missed).
    What exact parameters should be set, with which optimal values ?

  2. Now, once the client code is correctly set up with respect to 1), how should be managed the case where the server disconnects the client, but it is not the fault of the client (for example the server is experiencing issues).
    What parameters should be set in the client code, so that it reconnects to the server, without any constraint of retry limit (as the connection needs to run eternally) ?

@a412h a412h changed the title Question about maintaining an eternal connection Questions about maintaining an eternal connection Dec 20, 2024
@lws-team
Copy link
Member

You should look at the Secure Streams version of the same example.

In the -lowlevel version, the app deals with a wsi which represents a connection. The wsi is destroyed when the connection fails or ends. Retrying this with a new wsi when needed is the job of the app.

In the -ss version, the app deals with a higher level object that represents the endpoint (via JSON which describes how to connect to the endpoint, including the rules for reconnecting). The wsi representing a particular connection attempt is "inside" the Secure Streams object, such that the SS object outlives any particular connection, and is in a position to auto-retry, since it isn't destroyed like the wsi when the connection attempt fails or ends.

@a412h
Copy link
Author
a412h commented Dec 21, 2024

Thanks for the explanation about the difference between the -lowlevel and -ss versions.
However, I have no idea which one should be preferred for my use case.
To add some context, I am using a very minor modification of the -lowlevel example, to connect to a server that requires a ping every 60 seconds and for some streams requires authentication. With such stream, everything works perfectly with no error message, but after around 12 hours, the client does not receive messages anymore (again without any error showing up, so I guess the ping part is still correctly functioning).
So my fix to this is to setup a timer and after a certain amount of time, do:
return -1;
but I guess this is not best practice, as the disconnection - reconnection implies missing the data that arrives in between.
The same client code with the same server, but for a non authenticated stream does not produce the issue (messages still arrive after 12 hours). I have not found any mention of a duration limit of an authenticated connection from the server documentation, so I guess the problem is elsewhere.
Would you have any advice about the best practice I should follow in this case ?

@lws-team
Copy link
Member

Nothing you can do at the client can avoid data loss during downtime. You can have n connections from different places that are combined uniquely to try to reduce it, but in the end the server itself can have downtime.

I explained why you should look at the ss version. Lws can detect connections becoming invalid (not passing valid data for some timeout) and auto-retry the connection.

https://github.com/warmcat/libwebsockets/blob/main/READMEs/README.lws_retry.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0