Server-Side

Example: Below is the code example of the server side: 

Javascript
const express = require('express');
const socketIO = require('socket.io');
const http = require('http')
const port = process.env.PORT || 3000
var app = express();
let server = http.createServer(app);
var io = socketIO(server);

// make connection with user from server side
io.on('connection',
    (socket) => {
        console.log('New user connected');
        //emit message from server to user
        socket.emit('newMessage',
            {
                from: 'jen@mds',
                text: 'hepppp',
                createdAt: 123
            });

        // listen for message from user
        socket.on('createMessage',
            (newMessage) => {
                console.log('newMessage', newMessage);
            });

        // when server disconnects from user
        socket.on('disconnect',
            () => {
                console.log('disconnected from user');
            });
    });

app.get("/",
    (req, res) => {
        res.sendFile(__dirname + "/client-side.html");
    });

server.listen(port);

Output:

Web-Socket in Node

Similar Reads

What is a Web Socket?

WebSocket is a communication protocol enabling full-duplex communication, allowing simultaneous two-way communication between a user’s browser and the server. It establishes a continuous connection, enabling messages to be sent between the web server and browser at any time. Unlike traditional request/response formats, WebSocket facilitates server-initiated communication with the client. To implement WebSocket in NodeJS, the “socket.io” dependency needs installation. Additionally, installing the “express” module is essential for server-side applications....

Approach:

Server-Side (Node.js):Requires and configures necessary modules: express, socket.io, and http.Creates an HTTP server using express and http.Sets up a WebSocket connection using socket.io.Handles user connections, emits an initial message, listens for user messages, and handles disconnections.Client-Side (HTML/JS):Creates a simple HTML form with an input field and a submit button.Includes the Socket.IO library using .Establishes a WebSocket connection with the server upon page load.Logs a message on successful connection and sets up listeners for incoming messages.Emits a ‘createMessage’ event when the form is submitted, preventing the default form behavior.Logs messages from the server and disconnection events.Communication Flow:Server emits an initial ‘newMessage’ to the user upon connection.User’s form submission triggers the emission of a ‘createMessage’ event to the server.Server logs the incoming message from the user.Both the server and user log disconnection events.Routing:Server responds to the root route (“/”) by sending the HTML file (client-side.html in this case).Additional Notes:The server-side script (app.js) and the client-side HTML are separated.Console logs on both the server and client provide insights into the connection, messages, and disconnections....

Server-Side:

Example: Below is the code example of the server side:...

Client-Side:

Example: Below is the code example of the client side:...