Skip to content

Instantly share code, notes, and snippets.

@TakesTheBiscuit
Created April 19, 2023 20:35
Show Gist options
  • Save TakesTheBiscuit/bba8839b24477523cc66c5a8d28f636c to your computer and use it in GitHub Desktop.
Save TakesTheBiscuit/bba8839b24477523cc66c5a8d28f636c to your computer and use it in GitHub Desktop.
websocket.js using node and ws package
'use strict';
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
ws.on('message', function message(data) {
console.log('received: %s', data);
});
ws.send('something');
setInterval(() => {
const dateNow = new Date();
ws.send(`something @ ${dateNow.toISOString()}`);
}, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment