reepicheepR
tRPC3y ago
4 replies
reepicheep

What is the pattern for unsubscribing from a subscription?

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);
    },
  });


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

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