tRPCttRPC
Powered by
reepicheepR
tRPC•3y 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);
    },
  });
  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})`);
  });
});
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Subscription for a specific user
cha0sg0dCcha0sg0d / ❓-help
2y ago
What is wrong with my subscription setup?
OdielOOdiel / ❓-help
15mo ago
tRPC subscription : Access to socket ID from subscription
ChronicStoneCChronicStone / ❓-help
3y ago