Blog>
Snippets

Inter-Saga Communication Using Generic Channels

Illustrate how to use generic channels for direct message passing between two sagas, including sending and receiving messages.
import { channel } from 'redux-saga';
import { take, put, call } from 'redux-saga/effects';
Import necessary effects and the channel factory from redux-saga.
function* sagaOne(chan) {
    while (true) {
        const message = yield take(chan);
        console.log('Message received in Saga One:', message);
    }
}
Defines sagaOne that listens for messages on the channel and logs them.
function* sagaTwo(chan) {
    yield put(chan, 'Hello from Saga Two!');
}
Defines sagaTwo that sends a message to sagaOne through the channel.
function* mainSaga() {
    const chan = yield call(channel);
    yield call(sagaOne, chan);
    yield call(sagaTwo, chan);
}
The main saga creates a channel and starts both sagaOne and sagaTwo, passing the channel to them for direct communication.