denoland / denoland/std

proposal: bring back the `ws` module

Open
#5,755 2 comments 3 reactions 0 assignees View on GitHub
feedback welcome
Dominant language
TypeScript
Stars
3.6k
Forks
681
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**

Originally brought up by @lowlighter [on the discord](https://discord.com/channels/684898665143206084/775393009981849600/1275355929726156820).

> is there something like a websocket standard framework ?
> that would provide helpers/utilities like rooms, grouped/directed messaging, autoreconnect, etc. ?
a bit like socket.io was offering (seems overkill to use it as native ws are pretty capable on their own now

One of the biggest libraries in the npm ecosystem is `socket.io`, for good reason. `socket.io` bridged the gap between websockets and long polling when websockets weren't mature enough in browsers. Nowadays, websockets are available everywhere so a lot of the complicated abstractions that `socket.io` relied on are no longer useful.

Today, websockets are almost perfect except for a few things:
- Client side: No autoreconnect, the logic is a little complicated but all applications share this same need
- Server side: No rooms, no grouped messaging, no safety around when can send to clients or not

This use case is so prevalent that `Bun` decided to implement it natively (not that I necessarily approve):

```ts
Bun.serve({
fetch(req, server) {}, // upgrade logic
websocket: {
message(ws, message) {}, // a message is received
open(ws) {}, // a socket is opened
close(ws, code, message) {}, // a socket is closed
drain(ws) {}, // the socket is ready to receive more data
},
});
```

**Describe the solution you'd like**

I'd love to see something similar to `Bun` or `socket.io` built into the std that address the above usecases, both for the **client** and the **server**.

**Describe alternatives you've considered**

We could just tell people to reimplement the logic themselves every time, but I've definitely gotten tired of that.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.