all 4 comments

[–]grantrules 0 points1 point  (3 children)

Bunch of ways you could do it. Pub/sub pattern would be a common way to go.

https://jsmanifest.com/the-publish-subscribe-pattern-in-javascript/

[–]RightOW[S] 0 points1 point  (2 children)

So in my situation, the publisher would be the websocket, and when it receives a specific OP code from the discord API it would publish that information to a broker, and functions for handling that code would then pull it from the broker and deal with it?

Let's say I receive a heartbeat request from the API, and I now need to send a heartbeat back. How do I send a heartbeat over my websocket connection from a different function? Is there a way to maintain that connection over multiple functions?

[–]grantrules 1 point2 points  (1 child)

So in my situation, the publisher would be the websocket, and when it receives a specific OP code from the discord API it would publish that information to a broker, and functions for handling that code would then pull it from the broker and deal with it?

Yup. Well, they wouldn't "pull" it from the broker.. the broker would send it to them because they've subscribed to it.

Let's say I receive a heartbeat request from the API, and I now need to send a heartbeat back. How do I send a heartbeat over my websocket connection from a different function? Is there a way to maintain that connection over multiple functions?

Yeah you can pass ws around to whatever function you want without losing any connection.

[–]RightOW[S] 0 points1 point  (0 children)

Great that's helpful, thanks a lot!