Repository navigation
Description
Describe the feature you'd like to request
I'm looking to perform an operation on a subscription when a websocket reconnects.
I have clients subscribing to a stream of events. I am sending the id of the last event received; my server will only emit events created after the given event id.
let lastEventId = getFromLocalState()
client.join.subscribe({ lastEventId }, {
onData(event) {
lastEventId = event.id
},
})However, whenever the websocket reconnects, the same lastEventId value that was used when the subscription was initially created is re-sent to the server. My server then re-sends events that we have already received. This occurs both when my server restarts & sends broadcastReconnectNotification as well as when the websocket is interrupted client-side.
Describe the solution you'd like to see
I'm looking for a reconnect handler that will allow me to define the subscription's behavior on re-connect.
It could have the following options:
- Provide a mechanism to re-evaluate the subscription parameters once the connection to the websocket is re-established.
- Provide a mechanism to not re-establish the subscription when a websocket is disconnected.
Some rough ideas....
let lastEventId = getFromLocalState()
client.join.subscribe({ lastEventId }, {
onData(event) {
lastEventId = event.id
},
onReconnect() {
// return the new arguments for the resumed subscription
return { lastEventId }
}
})let lastEventId = getFromLocalState()
const { unsubscribe } = client.join.subscribe({ lastEventId }, {
onData(event) {
lastEventId = event.id
},
onDisconnect() {
// close this subscription
unsubscribe()
// create a new subscription with the latest parameters
client.join.subscribe({ lastEventId }, { ... })
}
})Describe alternate solutions
This issue is similar to #1574, which proposes re-evaluating subscription parameters all the time. I think this would solve the problem but doesn't give the subscription any insight into the state of the websocket.
(The workaround described in the description doesn't work for me, as onError is not dispatched when broadcastReconnectNotification is invoked.)
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Activity
mgreystone commentedon Apr 4, 2023
Open discussion: #4127
Elvincth commentedon Apr 9, 2023
+1 for this
mozzius commentedon Apr 18, 2023
Could the first argument of
subscribeoptionally be a function, likeuseState? You'd call it each time a connection is established to get the latest params.[-]feat: Websocket Reconnect Events[/-][+][TRP-17] feat: Websocket Reconnect Events[/+][-][TRP-17] feat: Websocket Reconnect Events[/-][+]feat: Websocket Reconnect Events[/+]sneko commentedon Feb 19, 2024
I'm also looking for reactivity on parameters change over subscriptions. Or a way to have some control over it, like a
.stop()so I could try to hack a bit.@mgreystone did you find a workaround in the meantime?
mgreystone commentedon Feb 22, 2024
@sneko
Investigated what it would take to expose some visibility of the websocket state to the subscriber. But i found that the subscriber & the websocket client are (reasonably) kept pretty distant from each other,
& it would take a lot of changes to make this work.
So i did end up with a workaround. When the subscriptions
onStartedcallback is invoked (which will happen every connection/reconnection), i call aresumemutation with whatever state my client was last in. The server then receives that state & starts pushing changes to the browser over the websocket subscription.So something like...
server
browser
KATT commentedon Nov 2, 2026
🤖 Supported in v11: yield events with
tracked()andwsLinkre-sends the lastlastEventIdin the input when it reconnects.Docs: https://trpc.io/docs/server/subscriptions#tracked. Closing.
github-actions commentedon Nov 4, 2026
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.