T
tRPC

What is the pattern for unsubscribing from a subscription?

What is the pattern for unsubscribing from a subscription?

Rreepicheep10/23/2023
Hi there! I'd like to prevent unnecessary connections to a websocket server across multiple renders. What is the correct pattern to ensure this?
api.live.onNewIncident.useSubscription(undefined, {
onData: () => {
ctx.incidents.all.invalidate().catch(console.error);
},
});
api.live.onNewIncident.useSubscription(undefined, {
onData: () => {
ctx.incidents.all.invalidate().catch(console.error);
},
});
wss.on("connection", (ws) => {
console.log(`++ Connection (${wss.clients.size})`);

ws.once("close", () => {
console.log(`-- Connection (${wss.clients.size})`);
});
});
wss.on("connection", (ws) => {
console.log(`++ Connection (${wss.clients.size})`);

ws.once("close", () => {
console.log(`-- Connection (${wss.clients.size})`);
});
});

Looking for more? Join the community!

T
tRPC

What is the pattern for unsubscribing from a subscription?

Join Server