-
-
Notifications
You must be signed in to change notification settings - Fork 619
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
feat(WebSocket): Add Pub/Sub feature #3466
base: main
Are you sure you want to change the base?
Conversation
It looks very good to me. I like it. I have an idea. const chatroom1 = new PubSub()
app.get(
'/ws',
upgradeWebSocket((c) => {
return {
onMessage(event, ws) {
chatroom1.publish('Hello World')
},
pubSub: chatroom1
}
})
) I think it's more extendable. And in my opinion, I think we have to avoid extending |
Hmm. Your opinion was not bad for me. However, most of actual use cases need multiple topics, so there is a risk that the completed code will become complicated. app.get(
'/ws/:id',
upgradeWebSocket((c) => {
return {
onMessage(event, ws) {
const { id } = c.req.param()
pubsub.subscribe(`chatroom-${id}`) // we want to do this
}
}
})
) Individually, also in the context of code compatibility, I feel we should follow Bun's Pub/Sub API as possible. |
I agree with you. // Bun
const {
upgradeWebSocket,
websocket,
pubSub // Provides pubSub instance for Bun
} = createBunWebSocket<ServerWebSocket>()
app.get(
'/ws/:id',
upgradeWebSocket((c) => {
const { id } = c.req.param()
return {
onOpen(_evt, ws) {
ws.subscribe(`chatroom-${id}`) // Use received `pubSub`
},
onMessage(event, ws) {
pubsub.publish(`chatroom-${id}`, event.data)
},
pubSub
}
})
) // Other runtimes
const pubSub = new PubSub()
app.get(
'/ws/:id',
upgradeWebSocket((c) => {
const { id } = c.req.param()
return {
onOpen(_evt, ws) {
ws.subscribe(`chatroom-${id}`) // Use received `pubSub`
},
onMessage(event, ws) {
pubsub.publish(`chatroom-${id}`, event.data)
},
pubSub
}
})
) In this idea, users can switch runtime easier. And it doesn't extends WSContext and WSEvents for only Bun. |
I like this when we will have this? |
when we can have this ? |
@moshOntong-IT To bypass it, you can write code like const wses = new Set()
app.get('/ws', upgradeWebSocket(() => ({
onOpen(_, ws) {
wses.add(ws)
},
onClose(_, ws) {
wses.delete(ws)
}
})))
// Publish
for (const ws of wses) {
ws.send('Hello')
} |
I am using bun is this still compatible? |
Yes! |
I think Bun Pub/Sub API will work fine for you than Set object. import { Hono } from 'hono'
import { createBunWebSocket } from 'hono/bun'
import type { ServerWebSocket } from 'bun'
const app = new Hono()
const { upgradeWebSocket, websocket } =
createBunWebSocket<ServerWebSocket>()
app.get(
'/ws',
upgradeWebSocket((c) => {
return {
onMessage(event, ws) {
ws.raw.subscribe('chatroom')
server.publish('Hello!')
}
}
})
)
const server = Bun.serve({
fetch: app.fetch,
websocket,
}) |
This is actually works but how can I access the server in different file? |
relate to #3230
Desctiption
This PR will allow us to use Publish/Subscribe messaging model on WebSocket like Bun can do natively.
Demo
Bun
Bun has own Pub/Sub API.
Other runtimes
Other runtimes such as Deno will get minimal PubSub class.
I don't know this method of implementation will accept all people. So if you have the opinion, please comment.
The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code