H
Hono6mo ago
Blankeos

Have you guys ever gotten websockets to work on Hono + `@hono/vite-dev-server` plugin?

This occurs for Bun and Node. 1. On Bun, I can serve websockets using import { createBunWebSocket } from 'hono/bun'; https://github.com/honojs/vite-plugins/issues/148 2. On Node, I can serve websockets using ws and import { serve } from '@hono/node-server' __ But the issues are: 1. On Bun, the websocket handler gets stuck. No idea why: https://github.com/honojs/vite-plugins/issues/140. Had some hypothesis that it conflicts with vite, but not too sure. 2. On Node, I can't even run the websocket handler because the mechanism is completely different when running it in dev. In particular, you'd have to get the server first, then attach it there.
1 Reply
mau
mau5mo ago
i was able to get it to work but the issue was facing was with headers. here is sample code...
app.get(
'connect',
upgradeWebSocket((c) => {
return {
onMessage(event, ws) {
// console.log(`Message from client: ${event.data}`);
// let data = { ...event.data, event: 'send' };
console.log(event);
ws.send(event.data);
},
onClose: () => {
console.log('Connection closed');
},
onOpen: (e) => {
console.log('open', e);
},
};
})
);
app.get(
'connect',
upgradeWebSocket((c) => {
return {
onMessage(event, ws) {
// console.log(`Message from client: ${event.data}`);
// let data = { ...event.data, event: 'send' };
console.log(event);
ws.send(event.data);
},
onClose: () => {
console.log('Connection closed');
},
onOpen: (e) => {
console.log('open', e);
},
};
})
);
If i add http routes and i want to set cors etc it starts giving me error I finally made a decision to use partykit... i am exploring and trying to see if i like the abstraction or not.
Want results from more Discord servers?
Add your server