Setup
Before we dive into logging WebSocket events, let’s set up a basic WebSocket server using the ws library in Node.js. Open your favorite terminal and follow these steps:
- Create a new directory for your project:
mkdir websocket-logger cd websocket-logger - Initialize a new Node.js project and install the
wspackage:npm init -y npm install ws -
Create a new file called
server.jsand add the following code:const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { ws.on('message', (message) => { console.log('Received message:', message); }); ws.on('close', () => { console.log('WebSocket connection closed'); }); }); - Run the server:
node server.js
Now we have a WebSocket server listening on port 8080. Any messages received will be logged to the console.
Logging WebSocket Events
To log WebSocket events in real-time, we can modify the server code to listen for additional event types. Let’s add logging for the open and error events.
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
console.log('Received message:', message);
});
ws.on('close', () => {
console.log('WebSocket connection closed');
});
ws.on('open', () => {
console.log('WebSocket connection opened');
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
});
With these additional event handlers, we can now log the open and error events along with the existing message and close events.
Real-time Logging
To view the WebSocket events in real-time, we can use a logging library like winston and configure it to log to a file. Here’s how to set it up:
- Install
winstonas a dependency:npm install winston -
Update the
server.jsfile:const WebSocket = require('ws'); const winston = require('winston'); const logger = winston.createLogger({ transports: [ new winston.transports.File({ filename: 'websocket.log' }), ], }); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { ws.on('message', (message) => { logger.info('Received message:', message); }); ws.on('close', () => { logger.info('WebSocket connection closed'); }); ws.on('open', () => { logger.info('WebSocket connection opened'); }); ws.on('error', (error) => { logger.error('WebSocket error:', error); }); }); - Run the server again:
node server.js
Now, every WebSocket event will be logged to the websocket.log file in real-time.
Conclusion
Logging WebSocket events in real-time is crucial for monitoring and debugging WebSocket connections. By listening to the open, message, close, and error events, we can log important information about the connection. Additionally, using a logging library like winston allows us to store the logs and analyze them later if needed.
Start logging your WebSocket events today and gain valuable insights into your real-time communication system.
#WebSocket #NodeJS