Skip to content

Instantly share code, notes, and snippets.

@jamigibbs
Last active March 30, 2020 19:10
Show Gist options
  • Save jamigibbs/a0a5a8488ac7e07f203e6870014a7ab0 to your computer and use it in GitHub Desktop.
Save jamigibbs/a0a5a8488ac7e07f203e6870014a7ab0 to your computer and use it in GitHub Desktop.
Node Websocket Server Base
'use strict';
const express = require('express');
const socketIO = require('socket.io');
const PORT = process.env.PORT || 3000;
const INDEX = '/index.html';
const server = express()
.use((req, res) => res.sendFile(INDEX, { root: __dirname }))
.listen(PORT, () => console.log(`Listening on ${PORT}`));
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('Client connected');
socket.on('disconnect', () => console.log('Client disconnected'));
});
setInterval(() => io.emit('time', new Date().toTimeString()), 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment