Subscribing to websocket events
See original GitHub issueHi. I would really appreciate some clarification om how to handle my case. Sorry for a bit lengthy explanation, but I’d like to describe every bit involved.
I have several sagas:
- XHR API middleware that takes the API request action, calls the API, waits for the promise to resolve, etc. Pretty standard and I have no issues with it. It uses an isolated “service” module to make the actual XHR calls.
- Websocket middleware that has 3 different sagas: connect, subscribe, emit. It uses an isolated service (which essentially wraps the socket.io-client lib). The
connect
method returns a promise (and works OK). Theemit
method is synchronous, works fine as well. Thesubscribe
method is the one that baffles me. The issue here is that events from the socket can happen multiple times, so I cannot promisify this method. See below for how I use it. - The actual “meat” saga that has to do the following:
take
the XHR success event for a specific resource (chat rooms list), subscribe to the websocket events for the new messages, and emit the subscription websocket event (that will actually put the current client into the listeners list for the given char rooms on the server).
Now here’s how the latter saga looks:
export default function* myRooms(getState) {
while (true) {
yield take(MY_ROOMS_SUCCESS) // works OK
yield put(subscribeSocket({ // it's sent and taken just fine in the other saga
event: 'newMessage', // the name of the socket.io event to subscribe to
actionType: MY_ROOMS_NEW_MESSAGE // the name of the action to dispatch
}))
const state = getState()
const roomIds = ... // get the IDs of the chat rooms
yield put(emitSocket({
event: 'subscribe', // the name of the socket.io event to emit
payload: roomIds
}))
}
}
Now for the socket subscription saga:
function* subscribeSocket(getState) {
while (true) {
const nextAction = yield take(SUBSCRIBE_SOCKET)
const subscribeConfig = nextAction.payload
const { event, actionType } = subscribeConfig
const callback = (payload) => put({ type: actionType, payload }) // <-- here
const unsibscribe = socket.subscribe(event, callback)
}
}
So the subscription saga gets the socket event name to listen to, and the action type name to dispatch together with the event payload.
I can tell from the debugging that the callback
is actually executed. As far as I understand the problem here is the improper put
usage.
The question is: what is the proper usages of sagas in this case? I want to dispatch the actionType
every time the callback
is called.
Thanks in advance!
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:16 (6 by maintainers)
Top Results From Across the Web
Using WebSocket subscriptions without queues
In this post, you can learn about the WebSocket Protocol and how to use WebSocket subscriptions without queues.
Read more >how to capture subscribe event in my webSocket server with ...
It works, but captures all possible events, I don't think it is a good practice. So, my question can be rephrased: how to...
Read more >Subscribing to events via Websocket - Tendermint Core
Tendermint emits different events, which you can subscribe to via Websocket (opens new window). This can be useful for third-party applications (for analysis) ......
Read more >Handling WebSocket Events | Twitch Developers
To discover the events that a WebSocket session subscribes to, use the Get EventSub Subscriptions endpoint. For details, see Getting the list of...
Read more >WebSocket: message event - Web APIs | MDN
The message event is fired when data is received through a WebSocket.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
For what it’s worth, I got around to testing my code and it was almost correct. Here’s a working version with
eventChannel
@emirotin I think this maybe related to this SO question. The main point is
It joins somewhat your solution, but factor out the concept into a separate function. So the saga can be easily tested. You can find a jsbin live demo here https://jsbin.com/wumuri/edit?js,console