Skip to content

Instantly share code, notes, and snippets.

@Scarysize
Last active October 5, 2016 10:34
Show Gist options
  • Save Scarysize/f8acd949b499c63a9bbbf79950679700 to your computer and use it in GitHub Desktop.
Save Scarysize/f8acd949b499c63a9bbbf79950679700 to your computer and use it in GitHub Desktop.
/**
* Creates a redux middleware which will send every action over a websocket connection.
* @param {WebSocket} socket The websocket connection object
* @return {Function} A redux middleware function
*/
function createSyncMiddleware(socket) {
return () => next => action => {
// your actions should not contain any functions, which can´t be stringified
socket.send(JSON.stringify(action));
// don´t forget this otherwise the middleware will swallow all actions
next(action);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment