Skip to content
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

premature closing of websocket #3

Closed
s13rw81 opened this issue May 31, 2021 · 6 comments
Closed

premature closing of websocket #3

s13rw81 opened this issue May 31, 2021 · 6 comments
Labels
wontfix This will not be worked on

Comments

@s13rw81
Copy link

s13rw81 commented May 31, 2021

Greetings,

I have observed that the web socket is automatically closed after a certain interval of time.

When we subscribe to certain scripts that have very low trading going on, ticks for these symbols may come minutes apart sometimes. I suspected that the Yahoo servers may be seeing no activity for these periods and actively closing the web socket, so I added an market index to the subscription list which has very good tick flow.

This too resulted in the web socket closing automatically. I was hoping there would be a way to keep the web socket alive for the duration of the market ( 6 hours 30 minutes to be precise ), so that I will not have to restart my code every time it fails.

Seeking your suggestions and advise on this.

Regards

@mbnlc mbnlc added the bug Something isn't working label May 31, 2021
@mbnlc
Copy link
Owner

mbnlc commented May 31, 2021

Hey Sourav!

I have encountered this issue before and I also think it is due to inactivity. If I remember correctly, the web socket connection closes after about 20 minutes to prevent the Yahoo! Finance webpage from using resources when the client is inactive.

Personally, I haven't found any way around this yet, other than manually resetting the web socket.

Best regards

@mbnlc
Copy link
Owner

mbnlc commented May 31, 2021

I just took a look at the Yahoo! Finance data disclaimer and it states that "Quotes are updated automatically but will be turned off after 25 minutes of inactivity."

https://help.yahoo.com/kb/finance-for-web/SLN2310.html?locale=en_US

@s13rw81
Copy link
Author

s13rw81 commented Jun 6, 2021

Greetings,

So when the connection is closed, trying to access the quote object throws an WebSocketConnectionClosedException exception. It is just a matter of catching this exception and within it re-subscribing to the symbols and re-starting the ws once again and quotes start streaming almost seamlessly.

Posting this if here if a beginner like me encounters the same issue in the future.

Regards

@mbnlc
Copy link
Owner

mbnlc commented Jun 7, 2021

Hello,

That's a really great fix!

I'm currently quite busy with other work, so I won't be able to implement this into the package too quickly. If you're interested in contributing, feel free to open a pull request.

Great find!

Best regards

@s13rw81
Copy link
Author

s13rw81 commented Jun 10, 2021

Greetings,

I was able to solve this without requiring to modify your library. I took a peek under the hood and found that there already exists a mechanism to handle any ws errors.

self.qs.on_quote = self.quote_handler
self.qs.on_error = self.error_handler

By making use of the error_handler I was able to restart the ws immediately whenever the connection was terminated.

I have tested this for a couple of days, no problems so far.

Issue can be happily closed.

Thank you for this library.

@mbnlc
Copy link
Owner

mbnlc commented Jun 13, 2021

Hello!

I just released a new version. I intentionally did not add a method to automatically resubscribe out of recursion concerns.

None the less, the package now exposes the callback methods: on_connect, on_quote, on_error and on_close to the user for easier use. As the versioning suggests, this is a major update with breaking changes.

For anyone looking to solve the same issue as @s13rw81, I recommend the solution posted above.

Hope this helps.

@mbnlc mbnlc added wontfix This will not be worked on and removed bug Something isn't working labels Jun 13, 2021
@mbnlc mbnlc closed this as completed Jun 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants