How to use this module with Websockets & subscriptions ? #95
Unanswered
michael-dm
asked this question in
Q&A
Replies: 2 comments 2 replies
-
seems to be unimplemented |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can do this now, just follow this doc from tRPC docs and use a Nitro plugin for the WS server: import { applyWSSHandler } from '@trpc/server/adapters/ws';
import WebSocket, { WebSocketServer as WSWebSocketServer } from 'ws';
import { appRouter } from '~/server/api/trpc/[trpc]';
export default defineNitroPlugin((nitro) => {
const WebSocketServer = WebSocket.Server || WSWebSocketServer;
const wss = new WebSocketServer({
port: 3002,
});
const handler = applyWSSHandler({ wss, router: appRouter });
wss.on('connection', (ws) => {
console.log(`➕➕ Connection (${wss.clients.size})`);
ws.once('close', () => {
console.log(`➖➖ Connection (${wss.clients.size})`);
});
});
console.log('✅ WebSocket Server listening on ws://localhost:3002');
nitro.hooks.hookOnce('close', async () => {
handler.broadcastReconnectNotification();
wss.close();
})
}) Here's a basic example - https://github.com/wobsoriano/trpc-nuxt-subscriptions-demo |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions