ChronicStone
ChronicStone
TtRPC
Created by ChronicStone on 8/28/2023 in #❓-help
tRPC subscription : Access to socket ID from subscription
I strongly inspired from the ideas presented in this article : https://preciselab.io/trpc/
5 replies
TtRPC
Created by ChronicStone on 8/28/2023 in #❓-help
tRPC subscription : Access to socket ID from subscription
I managed to get this working
5 replies
TtRPC
Created by ChronicStone on 8/28/2023 in #❓-help
tRPC subscription : Access to socket ID from subscription
That's the idea of what I'm trying to do...
5 replies
TtRPC
Created by ChronicStone on 8/28/2023 in #❓-help
tRPC subscription : Access to socket ID from subscription
// wsServer.ts
const WebSocketServer = WebSocket.Server || WSWebSocketServer;
const wss = new WebSocketServer({
port: 3002,
});

const handler = applyWSSHandler({ wss, router: appRouter, createContext: () => ({}) as Context });

wss.on('connection', (ws) => {
consola.log(`➕➕ Connection (${wss.clients.size})`);
ws.once('close', () => {
consola.log(`➖➖ Connection (${wss.clients.size})`);
const storage = useStorage('db')
// HERE, SOMEHOW GET UNIQUE SOCKET ID
const socketId = ...
const linkedUserId = storage.get(socketId)

// If present linked user ID, mark user offline
if(linkedUserId) db.update(userOrganizationsTable)....

});
});

consola.log('✅ WebSocket Server listening on ws://localhost:3002');
// wsServer.ts
const WebSocketServer = WebSocket.Server || WSWebSocketServer;
const wss = new WebSocketServer({
port: 3002,
});

const handler = applyWSSHandler({ wss, router: appRouter, createContext: () => ({}) as Context });

wss.on('connection', (ws) => {
consola.log(`➕➕ Connection (${wss.clients.size})`);
ws.once('close', () => {
consola.log(`➖➖ Connection (${wss.clients.size})`);
const storage = useStorage('db')
// HERE, SOMEHOW GET UNIQUE SOCKET ID
const socketId = ...
const linkedUserId = storage.get(socketId)

// If present linked user ID, mark user offline
if(linkedUserId) db.update(userOrganizationsTable)....

});
});

consola.log('✅ WebSocket Server listening on ws://localhost:3002');
5 replies