Looking to capture all messages
See original GitHub issueSituation I’d like to hook into all websocket data events to develop middleware using your Centrifuge::_transport private websocket object. This would open the door for redux and other integrations.
Background In prior versions, I was doing something like below. The problem is that socket._transport is overwritten, removing my onmessage hook when the socket has a network issue. To overcome this, I’ve been reloading the page to re-apply my hook.
if (!socket._transportIsSet) {
socket._transportIsSet = true
const onCentrifugeMsg = socket._transport.onmessage
socket._transport.onmessage = function (msg) {
onCentrifugeMsg(msg)
try {
const data = JSON.parse(msg.data)
// Action responder:
const isAction = data.result?.data?.data
if (isAction) {
store.dispatch(isAction[1])
}
} catch (e) {
return
}
}
}
Assessment Without a way to hook into data streams reliably, there isn’t a good way to handle global side effects from the servers. For example, sending all channels of a specific “type” a popup message.
Ex: I want to send the data: [‘action’, { type: ‘ALERT’, ‘Free donuts in the lunch room!’ }] to all rooms starting with “staff:”.
This would require specific middleware that the library shouldn’t be concerned with.
Recommendation Expose a function in the library to allow a custom callback to be run during socket onmessage hook.
🤞
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
If you already have channels then having an event type inside payload should be enough I suppose. You can always put your middleware logic on top of available API anyway - since you have message handlers and can do whatever you want with incoming messages.
If I’m in the following rooms; “lobby” “session:302894” “location:support” “bin:385”
and I want to respond with the same API to “session:”, “location:”, and “bin:” but not “lobby” : you are saying that I have to create 7 rooms now and use: “lobby” “session:302894” “session-events:302894” “location:support” “location-events:support” “bin:385” “bin-events:385”
Even though my users and spin up and leave rooms instantly and dynamically? This will lead to a very complex class needed to maintain, hook, watch, close, and clean them up as they are opening and closing bin rooms. One bin room might have an error that only that bin should see; now I have to hope they are in the “bin-events” channel fast enough as the error came from “bin” events.