I'm new to javascript and ES6. How do I bind the websocket
object into the callback I supply to subscribe
? It feels wrong/wierd to me that I am accessing the websocket
object from "global" scope like this.
const URL = 'echo.websocket.org';
const websocket = new WebSocket(`wss://${URL}`);
websocket.onmessage = (event)=>redux.dispatch(ChatActions.receiveMessage(event.data));
redux.subscribe(() => {
const { lastAction } = redux.getState();
switch (lastAction.type) {
case ActionTypes.POST_MESSAGE:
return websocket.send(lastAction.text);
default:
return;
}
});