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

🎉 Add WebSocket resource #11

Merged
merged 1 commit into from
Oct 4, 2024
Merged

🎉 Add WebSocket resource #11

merged 1 commit into from
Oct 4, 2024

Conversation

cowboyd
Copy link
Member

@cowboyd cowboyd commented Oct 2, 2024

Motivation

The WebSocket API is one of the most common ways to communicate with servers over a stateful connection that allows you to both send and receive events async.

However, it is one of the most annoying things to do as a programmer because you have to coordinate between 4 separate callbacks: open, close, message, and error. How do you handle these callbacks properly given that they could be received in any order? You can either maintain an ad-hoc state machine and consult it within every callback, or you can use this contrib module which properly sequences the lifecycle of events into a single, coherent computation that is always resumed at the right point.

Approach

Use the WebSocket:

let socket = yield* useWebSocket("ws://websocket.example.org");

Already at this point, you're a leg up because the actual useWebSocket() operation does not return until the socket has connected and successfully received the open event. Furthermore, it also has installed a handler for the error event that will crash the resource if a connection error is received at any point.

Then, you can send data to the WebSocket just as you would normally.

socket.send("Hello World");

And finally, you can receive data from the socket as a stream of MessageEvents that is closed with a CloseEvent:

let subscription = yield* socket;
let next = yield* subscription.next();
while (!next.done) {
  let event = next.value; //MessagEvent
  console.log('received message', event.data);
  next = yield* subscription.next()
}
let closeEvent = next.value
console.log("closed", closeEvent.code)

There is no close() method on the web socket resource because it will be closed automatically whenever it passes out of scope.

Copy link
Member

@taras taras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@taras taras mentioned this pull request Oct 4, 2024
@cowboyd cowboyd force-pushed the cl/websocket branch 2 times, most recently from bc19e66 to c234113 Compare October 4, 2024 12:52
@cowboyd cowboyd marked this pull request as ready for review October 4, 2024 12:52
@cowboyd cowboyd merged commit df6d765 into main Oct 4, 2024
3 checks passed
@cowboyd cowboyd deleted the cl/websocket branch October 4, 2024 12:55
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

Successfully merging this pull request may close these issues.

2 participants