Hono supports Socket.io? #1781
Replies: 9 comments 10 replies
-
|
Hi @PatricioPoncini , So I came across to this problem as well last week. But, if you take a look at hono import { Server } from 'socket.io'
import { Server as HttpServer } from 'http'
import { Hono } from 'hono'
const app = new Hono()
const server = serve(app)
const io = new Server(server as HttpServer)Note that the type cast depends on the option you pass to Hope this helps :) |
Beta Was this translation helpful? Give feedback.
-
Bun is not written this way. |
Beta Was this translation helpful? Give feedback.
-
|
@czfadmin May I ask how to use I don't know how to implement similar functions in hono. I implemented it in Express like this: // server.ts
import express from 'express'
import http from 'http'
const app = express()
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: '*',
},
})
io.on('connection', (socket) => {
console.log('a user connected')
})
app.set('io', io)
app.use('/messages', authenticate, messageRouter)// messages.ts
import express from 'express'
const router = express.Router()
router.get('/', (
req: express.Request,
res: express.Response,
) => {
const io = req.app.get('io')
io.emit('message', 'hello1')
// ...
}) |
Beta Was this translation helpful? Give feedback.
-
|
Hey any update if it now fully supports Socket.io with Bun? |
Beta Was this translation helpful? Give feedback.
-
|
Any update till now ? |
Beta Was this translation helpful? Give feedback.
-
|
+1 for bun with socket.io support. |
Beta Was this translation helpful? Give feedback.
-
|
Related issue: socketio/socket.io#4969 |
Beta Was this translation helpful? Give feedback.
-
|
any work around ? any one |
Beta Was this translation helpful? Give feedback.
-
|
With Node.js: import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { Server } from "socket.io";
const app = new Hono();
const httpServer = serve({
fetch: app.fetch,
port: 3000,
});
const io = new Server(httpServer, {
/* options */
});
io.on("connection", (socket) => {
// ...
});Reference: https://socket.io/docs/v4/server-initialization/#with-hono-nodejs With Bun: import { Server as Engine } from "@socket.io/bun-engine";
import { Server } from "socket.io";
import { Hono } from "hono";
const io = new Server();
const engine = new Engine();
io.bind(engine);
io.on("connection", (socket) => {
// ...
});
const app = new Hono();
const { websocket } = engine.handler();
export default {
port: 3000,
idleTimeout: 30, // must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
fetch(req, server) {
const url = new URL(req.url);
if (url.pathname === "/socket.io/") {
return engine.handleRequest(req, server);
} else {
return app.fetch(req, server);
}
},
websocket
}Reference: https://socket.io/docs/v4/server-initialization/#with-hono--bun Source here: https://github.com/socketio/bun-engine |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone, I would like to know if Hono supports Socket.io, as I want to set up a socket service with Socket.io but I'm having issues with the ServerType. Since Socket.io only accepts one of these types:
http.Server | HTTPSServer | Http2SecureServer | numberAnd when I start a server with Hono, it returns the type
ServerTypeso the Socket.io library gives me problems because it does not accept that typing. Does anyone know how I can set up the server with Hono and pass it as an argument to Socket.io?Beta Was this translation helpful? Give feedback.
All reactions