David 👾D
tRPC8mo ago
1 reply
David 👾

Bun & Hono websocket

Has anyone successfully made bun websocket work with trpc ws handler?

I tried this but it doesn't work:
import { applyWSSHandler } from '@trpc/server/adapters/ws';
import type { ServerWebSocket } from 'bun';

...
const app = new Hono();
const { websocket } = createBunWebSocket<ServerWebSocket>();
...
app.use(
    '/trpc/*',
    trpcServer({
        router: appRouter,
        createContext: (_opts, c) => createTRPCContext(c.req.raw.headers),
    })
);

applyWSSHandler({
    wss: websocket,
    router: appRouter,
    createContext: (_opts) => createTRPCContext(_opts.req.headers),
    keepAlive: {
        enabled: true,
        pingMs: 30000,
        pongWaitMs: 5000,
    },
});
...
export default {
    fetch: app.fetch,
    websocket,
};


I found this https://github.com/cah4a/trpc-bun-adapter but I would like to do that with Hono only
Was this page helpful?