Transform any single-player HTML5 game into online multiplayer with Cerewro: a Node.js server with Socket.io syncs the game state in real time between all connected clients.
| Component | Technology | Role |
|---|---|---|
| Client | HTML5 Canvas + Socket.io-client | Renders game, sends inputs |
| Server | Node.js + Socket.io | State authority, syncs players |
| Protocol | WebSocket (via Socket.io) | Real-time bidirectional communication |
Add online multiplayer to the Snake game:
- Node.js + Socket.io server on port 3001
- Waiting room with invite code
- Up to 4 simultaneous players, each with different color
- 20 ticks/second state sync
- When a snake collides it's eliminated (others continue)
- Winner: last snake surviving
- Real-time chat with Bootstrap
- Auto-reconnection on connection loss
const { Server } = require('socket.io');
const io = new Server(http.createServer(express()), { cors:{origin:'*'} });
io.on('connection', socket => {
socket.on('createRoom', () => {
const code = Math.random().toString(36).substr(2,6).toUpperCase();
socket.join(code);
socket.emit('roomCreated', { code });
});
socket.on('direction', ({ code, dir }) => {
if (rooms[code]?.gameState?.snakes?.[socket.id])
rooms[code].gameState.snakes[socket.id].nextDir = dir;
});
});