Skip to content

Instantly share code, notes, and snippets.

@nsantorello
Created February 9, 2026 13:09
Show Gist options
  • Select an option

  • Save nsantorello/03641004e8393afe646149d9ff809429 to your computer and use it in GitHub Desktop.

Select an option

Save nsantorello/03641004e8393afe646149d9ff809429 to your computer and use it in GitHub Desktop.
Fuse websocket example
#!/usr/bin/env node
/**
* Test client for the Stats WebSocket Server
*
* Usage:
* node test-stats-client.js
*/
import { WebSocket } from 'ws';
const PORT = process.env.STATS_WS_PORT || 8889;
const HOST = process.env.HOST || 'localhost';
async function main() {
const wsUrl = `ws://${HOST}:${PORT}`;
console.log(`\nConnecting to ${wsUrl}\n`);
const ws = new WebSocket(wsUrl);
ws.on('open', () => {
console.log('✓ Connected to Stats WebSocket Server\n');
// Subscribe to all printers
console.log('Subscribing to all printers...');
ws.send(JSON.stringify({
type: 'subscribe',
printers: ['all']
}));
});
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
switch (message.type) {
case 'authenticated':
console.log('✓ Authenticated:', message.message);
break;
case 'snapshot':
console.log(`\n📸 Snapshot received: ${message.printers?.length || 0} printer(s)`);
message.printers?.forEach((printer, idx) => {
console.log(`\n Printer ${idx + 1}:`);
console.log(` Local ID: ${printer.printerId}`);
console.log(` Cloud ID: ${printer.cloudPrinterId || 'null'}`);
console.log(` State:`, JSON.stringify(printer.state, null, 2).split('\n').map((line, i) => i === 0 ? line : ' ' + line).join('\n'));
});
break;
case 'printer:state':
console.log(`\n📊 Printer State Update:`);
console.log(` Printer ID: ${message.printerId}`);
console.log(` Cloud ID: ${message.cloudPrinterId || 'null'}`);
console.log(` Timestamp: ${new Date(message.timestamp).toISOString()}`);
console.log(` State:`, JSON.stringify(message.state, null, 2).split('\n').map((line, i) => i === 0 ? line : ' ' + line).join('\n'));
break;
case 'printer:connected':
console.log(`\n✓ Printer Connected: ${message.printerId} (cloud: ${message.cloudPrinterId || 'null'})`);
break;
case 'printer:disconnected':
console.log(`\n✗ Printer Disconnected: ${message.printerId} (cloud: ${message.cloudPrinterId || 'null'})`);
break;
case 'ping':
// Respond to ping with pong
ws.send(JSON.stringify({ type: 'pong' }));
process.stdout.write('.');
break;
case 'error':
console.error(`\n❌ Error: ${message.message}`);
break;
default:
console.log(`\n⚠️ Unknown message type: ${message.type}`);
console.log(' Data:', JSON.stringify(message, null, 2));
}
} catch (error) {
console.error('Failed to parse message:', error.message);
console.error('Raw data:', data.toString());
}
});
ws.on('error', (error) => {
console.error('\n❌ WebSocket error:', error.message);
});
ws.on('close', (code, reason) => {
console.log(`\n\n✗ Connection closed (code: ${code}, reason: ${reason || 'none'})`);
process.exit(0);
});
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
console.log('\n\nClosing connection...');
ws.close();
});
}
main().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment