Skip to content

Instantly share code, notes, and snippets.

@Kinetic27
Last active November 15, 2025 08:53
Show Gist options
  • Select an option

  • Save Kinetic27/ad4f6bcc99bd521a2a1e923187fa7266 to your computer and use it in GitHub Desktop.

Select an option

Save Kinetic27/ad4f6bcc99bd521a2a1e923187fa7266 to your computer and use it in GitHub Desktop.
const WebSocket = require('ws');
const fs = require('fs');
const sampleData = JSON.parse(fs.readFileSync('sample-data.json', 'utf8'));
let currentIndex = 0;
const wss = new WebSocket.Server({ port: 8080 });
console.log('run in 8080');
wss.on('connection', ws => {
console.log('client connected');
const interval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(sampleData[currentIndex]));
currentIndex = (currentIndex + 1) % sampleData.length;
} else {
clearInterval(interval);
}
}, 500);
ws.on('close', () => {
console.log('bye');
clearInterval(interval);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment