You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider SSE to enable server->client http subscriptions #544
Reading through the #532 PR it occurred to me that the JSON-RPC notification system only works one way, from client to server. Most of the time you want it to be from server to client. Although nothing about the spec says which side is which, in a browser this basically means the browser is the client.
One interesting approach could be to use Server Sent Events. This would allow for stateless load balance-able subscriptions that could work with HTTP2. From what I'm seeing though I'm not sure if it would technically fall under a JSON-RPC complaint spec; though you probably can treat as a server to client notification. Nice thing is this is already built into browsers with polyfills available for node and most languages.
I think you'd create/cancel as you do currently but the client would also fetch on a new route /subscription/3nxu4ex234. Know this is separate from the websocket implementation but SSE isn't a well known. The major downside of the SSE spec as it stands is the text only protocol (compared to binary for websockets) but in a JSON-RPC context the point is moot.
SSE should be possible to add alongside WebSockets. I prefer WS myself as I don't have to think about max concurrent connection limit and my application doesn't have user numbers that make it hard to scale anyway.
I too love websockets, when they work. At scale have had issues with OOM memory causing a host of issues. The major issues at scale have been token based access timers, sticky load balancing, if a server goes down it takes everyone with it. I'll dig into the examples more but this was just to plant the idea in your head. 😃
Isn't scaling with SSE similar though, or am I missing something? It's still a persistent connection between a client and server(s) that also needs to be scaled.
Scaling is not my area of expertise, but with WS and the current setup it should scale seamlessly on things like k8s/render.com - we'll listen to a SIGTERM and broadcast a reconnect notification to all clients which will create an overlapping connection and send a ReconnectError to any listeners.
If you are handling reconnect that's great, the SSE spec has it built in. Since you are relying on ws it could be more reliable than the previous an earlier socket.io based approach.
Nice work @OutdatedVersion this is good stuff! I've been waiting/wanting SSE in tRPC for a while and this is an amazing start, however, I'm not sure a stream for every subscription like this is the best approach. Instead, I would expect a shared stream that has an accompanied endpoint to post sub/unsub topics is a better approach. This accompanied endpoint would just be handled by useSubscription or whatnot still.
@StringKe to my understanding yes, as I know so far AbortSignal is bugged/doesn't exist on the next 13.4.4 app route API handlers, I also wrote it down on one of the comments in the code. @seevercel/next.js#48682
Hey @matannahmani , I was using your repo implementation of tRPC + SSE as reference for a tRPC + SSE + svelte version, but since a few hours ago the repo is gone and all the links are broken, is there any way so we still have access for it as a reference? Thanks.
on a side note I will try to make a pull request to trpc main repo with The link and docs so people can view in the future for reference instead of having to search the issues every time, I see the progress on the SSELink is halted #4477
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.
Activity
KATT commentedon Jun 16, 2021
SSE should be possible to add alongside WebSockets. I prefer WS myself as I don't have to think about max concurrent connection limit and my application doesn't have user numbers that make it hard to scale anyway.
The way our HTTP subscriptions work right now is through long polling and you pass a cursor of the current state and server let's the request hang until there's new data / times out. Inspect https://chat.trpc.io/ to see how the long polling works.
delaneyj commentedon Jun 16, 2021
I too love websockets, when they work. At scale have had issues with OOM memory causing a host of issues. The major issues at scale have been token based access timers, sticky load balancing, if a server goes down it takes everyone with it. I'll dig into the examples more but this was just to plant the idea in your head. 😃
KATT commentedon Jun 16, 2021
Isn't scaling with SSE similar though, or am I missing something? It's still a persistent connection between a client and server(s) that also needs to be scaled.
KATT commentedon Jun 16, 2021
Scaling is not my area of expertise, but with WS and the current setup it should scale seamlessly on things like k8s/render.com - we'll listen to a
SIGTERMand broadcast a reconnect notification to all clients which will create an overlapping connection and send aReconnectErrorto any listeners.SIGTERMtrpc/examples/experimental-next-prisma-starter-websockets/src/server/wssDevServer.ts
Lines 18 to 22 in dcfff9e
ReconnectErrortrpc/packages/server/test/websockets.test.ts
Lines 439 to 484 in dcfff9e
delaneyj commentedon Jun 16, 2021
If you are handling reconnect that's great, the SSE spec has it built in. Since you are relying on
wsit could be more reliable than the previous an earlier socket.io based approach.delaneyj commentedon Jun 16, 2021
I'll close for now, will do more testing with the websocket approach until I have better reason to seek alternatives.
thelinuxlich commentedon Apr 25, 2022
graphql-yoga 2 added subscriptions exclusively through SSE, since HTTP/2 you don't have to care about max concurrent connections as it was in the past
KATT commentedon Apr 25, 2022
I'll reopen this and see if someone wants to work on it for the next major :)
31 remaining items
timcole commentedon Jun 3, 2023
Nice work @OutdatedVersion this is good stuff! I've been waiting/wanting SSE in tRPC for a while and this is an amazing start, however, I'm not sure a stream for every subscription like this is the best approach. Instead, I would expect a shared stream that has an accompanied endpoint to post sub/unsub topics is a better approach. This accompanied endpoint would just be handled by useSubscription or whatnot still.
Similar to how Twitter web client connects to https://api.twitter.com/live_pipeline/events which starts an eventstream with an optional
topicparameter of initial topics, then posts to https://api.twitter.com/1.1/live_pipeline/update_subscriptions with a comma-separated listsub_topicsandunsub_topicsto adjust the listening topics.There are many reasons why a single stream is best here but one main reason is the 100 connections per domain default limit with HTTP/2 and 6 connections on HTTP/1.1. If you have multiple things that need events and user has more than 1 tab open you'll hit the limit pretty quickly with multiple streams per event.
matannahmani commentedon Jun 4, 2023
@OutdatedVersion did you manage to make it work on Router handler (Next 13 app -> API).
I can't manage to convert it
https://github.com/OutdatedVersion/trpc-sse-link/blob/main/example/src/pages/api/trpc/%5Btrpc%5D.ts
the event's arent sent and request finishes immediately here is my attempt:
StringKe commentedon Jun 5, 2023
@OutdatedVersion unsubscription doesn't actually stop the Observable from running, does it?
matannahmani commentedon Jun 5, 2023
For people who may be interested I managed to make a production-ready version of SSE using app dir route handlers.
You can view the code here:
https://github.com/matannahmani/ada-trpc-sse-link
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/trpc/stream-link.ts
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/trpc/client.ts
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/app/api/trpc/%5Btrpc%5D/route.ts
Or view live here (integration using LangChain and TRPC SSE LINK):
https://preview.im-ada.ai
StringKe commentedon Jun 6, 2023
Will SSE continue to run on the back end when the user actively shuts it down?
matannahmani commentedon Jun 6, 2023
@StringKe to my understanding yes, as I know so far AbortSignal is bugged/doesn't exist on the next 13.4.4 app route API handlers, I also wrote it down on one of the comments in the code.
@see vercel/next.js#48682
marschr commentedon Jul 21, 2023
Hey @matannahmani , I was using your repo implementation of tRPC + SSE as reference for a tRPC + SSE + svelte version, but since a few hours ago the repo is gone and all the links are broken, is there any way so we still have access for it as a reference? Thanks.
matannahmani commentedon Jul 22, 2023
@marschr for sure, here is a new repo dedicated to SSE-Link, I will try to make some work during this weekend to showcase ways you can use it and ways we are using it (LangChain integration).
for now, you can see a snapshot from my earliest stable POC.
https://github.com/matannahmani/ada-trpc-sse-link
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/trpc/stream-link.ts
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/trpc/client.ts
https://github.com/matannahmani/ada-trpc-sse-link/blob/main/src/app/api/trpc/%5Btrpc%5D/route.ts
Or view live here (integration using LangChain and TRPC SSE LINK)
https://preview.im-ada.ai
on a side note I will try to make a pull request to trpc main repo with The link and docs so people can view in the future for reference instead of having to search the issues every time, I see the progress on the SSELink is halted #4477
github-actions commentedon Jun 23, 2024
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.