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.
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:
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 }, { ... })
}
})
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.)
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too