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

Websocket send() command #402

Closed
DoganM95 opened this issue Jan 10, 2025 · 6 comments
Closed

Websocket send() command #402

DoganM95 opened this issue Jan 10, 2025 · 6 comments

Comments

@DoganM95
Copy link

DoganM95 commented Jan 10, 2025

Hi, i just implemented a websocket connection, which sends messages over the socket like

ws = new WebSocket(url);
ws.on("open", () => {
    ws.send(JSON.stringify({ op: "subscribe", args: [`tickers.SOMETHINGUSDT`] }));
});

but refactored it to use your library, since yours handles auth, reconnects etc, which is great.
However, i cannot find a send() method in your WebsocketClient implementation, does it have a different name or am i missing something?

Thanks in advance!

@tiagosiebler
Copy link
Owner

tiagosiebler commented Jan 10, 2025

Hi, it works a little differently with my SDKs. Individual WebSocket connections don't live forever, so the SDK treats them as replaceable. The WS Client will automatically send heartbeats on any active connections and if a dead connection is detected, it will automatically tear down and replace the connection with a healthy one, before resubscribing to all topics you were already subscribed to.

This requires some state/knowledge (within the WS client) of which topics you've subscribed to, so it knows which ones to resubscribe to on reconnect, which is why all topic requests should be routed via the subscribeV5 method. There's also a unsubscribeV5 method:
https://github.com/tiagosiebler/bybit-api/blob/master/examples/ws-private-v5.ts#L63-L79

This is also why the individual websocket connections used by the WS client are not exposed directly (although you can access them if you really want to). For consuming events, you should use the event emitters on the WS client itself - which is an extra layer over it:
https://github.com/tiagosiebler/bybit-api/blob/master/examples/ws-private-v5.ts#L35-L38

If a reconnect happens, you don't need to resubscribe, you don't need to reconnect new event listeners - it's all automatic under the hood. In case you did want to check the REST API for anything you may have missed after the reconnect, there is a "reconnected" event indicating that it happened and successfully spawned a new connection to replace a dead one:
https://github.com/tiagosiebler/bybit-api/blob/master/examples/ws-private-v5.ts#L49-L51

If you're curious about how it works in practice, I highly recommend messing with your network connection while testing locally. Turn your wifi off for a while, or mess around with a VPN. No matter how long the interruption lasts, it should keep retrying until eventually respawning and resubscribing.

@DoganM95
Copy link
Author

Thanks for the fast and detailled response. I think i had to be more concise, the issue i have is not related to connection, but rather how to send (any) message to the server side from my client, using your websocket implementation.

So with default nodejs ws, i would do

ws.send(JSON.stringify({ op: "`something", args: [`something`] }));

Whereas with yours, maybe this method?

ws.trySend(ws.options.key, JSON.stringify({ op: "`something", args: [`something`] }))

@tiagosiebler
Copy link
Owner

Yes, you will want the tryWsSend(key, messageAsString) method:
https://github.com/tiagosiebler/bybit-api/blob/master/src/websocket-client.ts#L931

By the way, if this is regarding the WebSocket API that Bybit has, I'm currently working on upgrades in the WebSocket client to support it properly, including a REST-like way of using the WS API wrapped in promises. No exact time-line as there's a fair few "plumbing" changes I will need to thoroughly stress test, but I'm hoping to have it ready by the end of this month at the latest...

My gate.io SDK is actually the first one to see an implementation of a WS API and I'm likely to bring the same design over to the bybit SDK (as well as my other SDKs for exchanges that have a WS API). So if you'd like to see how that can work in practice, take a look at the gate SDK. Some docs here:
https://github.com/tiagosiebler/gateio-api/blob/master/README.md#websocket-api

Also an example here where you can see a minimal implementation in action:
https://github.com/tiagosiebler/gateio-api/blob/master/examples/ws-private-spot-wsapi.ts#L88-L95

It's a great time for feedback/concerns, if you'd like to influence how the final implementation will look/work, so if this is something you will use please do take a look and ideally try it out.

@tiagosiebler
Copy link
Owner

This is the thread to follow, if you want to stay updated on when it's ready: #344

@DoganM95
Copy link
Author

Thank you alot for all the info and quick responses! Will keep an eye on #344 and eventually use gate.io if that fits my needs

@tiagosiebler
Copy link
Owner

Thank you alot for all the info and quick responses! Will keep an eye on #344 and eventually use gate.io if that fits my needs

Emphasis on feedback! Any thoughts & concerns on the design implemented with gate so far are appreciated, since right now the one coming to Bybit will work in a similar way. It's the best time to influence it when it's not finished yet!

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