Skip to content

Instantly share code, notes, and snippets.

@isaacgr
Created September 15, 2019 14:51
Show Gist options
  • Save isaacgr/d0aa3c712130bb38b8d121675c283c99 to your computer and use it in GitHub Desktop.
Save isaacgr/d0aa3c712130bb38b8d121675c283c99 to your computer and use it in GitHub Desktop.
Websocket configuration with express and Herkou
'use strict';
const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX) )
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
});
setInterval(() => {
wss.clients.forEach((client) => {
client.send(new Date().toTimeString());
});
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment