Published on January 26, 2024By DeveloperBreeze

// Import 'ws' module
const WebSocket = require('ws');

// Create WebSocket Server on port 8080
const wss = new WebSocket.Server({ port: 8080 });

// Event listener for new connections
wss.on('connection', (ws) => {
  // Event listener for incoming messages
  ws.on('message', (message) => {
    console.log('Received:', message);
    // Send a response back to the client
    ws.send('Server: ' + message);
  });
});

Comments

Please log in to leave a comment.

Continue Reading:

File Stream Copy using 'fs' module

Published on January 26, 2024

javascript

Execute Shell Command using 'child_process' module

Published on January 26, 2024

javascript

Parse URL and Query Parameters

Published on January 26, 2024

javascript

Hashing Password with SHA-256 using 'crypto' module

Published on January 26, 2024

javascript

Create and Print Buffer

Published on January 26, 2024

javascript

Basic Authentication using 'express-basic-auth' middleware

Published on January 26, 2024

javascript

Construct File Path using 'path' module

Published on January 26, 2024

javascript

Event Emitter using 'events' module

Published on January 26, 2024

javascript

Set and Access Environment Variable

Published on January 26, 2024

javascript

Access Command-line Arguments

Published on January 26, 2024

javascript