20 lines
549 B
JavaScript
20 lines
549 B
JavaScript
import Server from 'socket.io';
|
|
|
|
export default function startServer(store) {
|
|
const io = new Server().attach(8090);
|
|
|
|
store.subscribe(
|
|
// Send 'state' events when updating state via Redux
|
|
() => io.emit('state', store.getState().toJS())
|
|
);
|
|
|
|
io.on('connection', socket => {
|
|
// Send state on connection
|
|
socket.emit('state', store.getState().toJS());
|
|
|
|
// Subscribe to action events sent by clients
|
|
// NOTE: this is dangerously insecure -- any client can trigger
|
|
// any action
|
|
socket.on('action', store.dispatch.bind(store));
|
|
});
|
|
} |