Skip to content

Instantly share code, notes, and snippets.

@praveen001
Last active July 22, 2019 11:16
Show Gist options
  • Save praveen001/c6b0b6eb24254829fdc2299e3c7ad69e to your computer and use it in GitHub Desktop.
Save praveen001/c6b0b6eb24254829fdc2299e3c7ad69e to your computer and use it in GitHub Desktop.
Redux Observable WebSocket
const connectEpic = (action$, state$: StateObservable<IState>) =>
action$
.ofType(WebsocketActionTypes.CONNECT)
.switchMap((action: IConnectWebsocketAction) =>
connectSocket(state$.value.config.webSocketURL)
.map(data => receiveMessageFromWebSocket(data))
.catch(e => of(disconnect(true))) // <-- Handle 1st case by using catch operator
);
const connectedEpic = action$ =>
action$.ofType(WebsocketActionTypes.CONNECT).switchMap(() =>
onOpenSubject.map(() => {
onCloseSubject.map(() => disconnect(true)); // <-- Handle 2nd case by listening to onCloseSubject
return connected();
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment