Skip to content

Instantly share code, notes, and snippets.

@AlexChittock
Last active November 7, 2017 21:21
Show Gist options
  • Save AlexChittock/dd27b0a162ee61df2cd7ba7352835af2 to your computer and use it in GitHub Desktop.
Save AlexChittock/dd27b0a162ee61df2cd7ba7352835af2 to your computer and use it in GitHub Desktop.
Simple server distributes 'action' messages between all connected clients.
import SocketIO from 'socket.io'
const io = SocketIO(3030)
const history = []
io.on('connection', (socket) => {
// Replay actions on new clients
socket.on('init', (_) => history.forEach(action => socket.emit('action', action)))
socket.on('action', (action) => {
// Persist actions in memory
history.push(action)
socket.broadcast.emit('action', action)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment