Skip to content

Instantly share code, notes, and snippets.

@praveen001
Last active July 22, 2019 11:14
Show Gist options
  • Save praveen001/64af3d21ce232bc31aafecaeed6e2abb to your computer and use it in GitHub Desktop.
Save praveen001/64af3d21ce232bc31aafecaeed6e2abb to your computer and use it in GitHub Desktop.
WebSocket Redux Observable
let webSocketSubject: WebSocketSubject<{}>;
let onOpenSubject = new Subject();
let onCloseSubject = new Subject();
// Create the websocket subject
const createSocket = websocketUrl => {
onOpenSubject = new Subject();
onCloseSubject = new Subject();
webSocketSubject = webSocket({
url: websocketUrl,
openObserver: onOpenSubject,
closeObserver: onCloseSubject
});
return webSocketSubject;
};
// Connect to web socket
const connectEpic = (action$, state$: StateObservable<IState>) =>
action$
.ofType(WebsocketActionTypes.CONNECT)
.switchMap((action: IConnectWebsocketAction) =>
createSocket(action.payload.websocketUrl)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment