Why use Node.js for real-time communication?

Table of contents

  • Why use Node.js for real-time communication?
  • Protocol and Library Types
  • How to ensure communication security?
  • code example
  • in conclusion

In today's digital world, real-time communication is essential. Whether it's a chat app or live sports updates, real-time communication is a must to keep users engaged. Node.js is a popular tool for developing real-time applications because of its speed, scalability, and reliability. In this article, we'll explore why Node.js is ideal for real-time communication and how to make it happen.

Why use Node.js for real-time communication?

Node.js is built on Google's V8 JavaScript engine, which is known for its high performance. This makes Node.js the perfect tool for building real-time communication applications that require speed and scalability. Node.js is also event-driven, meaning it can handle multiple connections simultaneously, making it the perfect tool for building real-time applications.

The benefits of this article, free C++ audio and video learning materials package, technical video/code, including (audio and video development, interview questions, FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, codec, push-pull stream, srs)↓↓↓ ↓↓↓See below↓↓Click at the bottom of the article to get it for free↓↓

Protocol and Library Types

Node.js provides several ways to achieve real-time data communication. Some common ways to communicate real-time data in Node.js include:

Socket.IO

Socket.IO is a popular real-time communication library. It uses WebSockets as the transport layer to provide real-time communication between client and server. Socket.IO provides many features such as automatic reconnection, support for binary data, and fallback options for environments that do not support WebSockets.

WebSockets

WebSockets is a protocol that supports real-time communication between clients and servers. It provides a full-duplex communication channel over a single TCP connection, allowing real-time data exchange between clients and servers. This module named "ws" can be used to implement WebSockets.

server sent event

Server-Sent Events is a simple protocol that allows servers to send events to clients over an HTTP connection. It is ideal for applications that require one-way communication, such as live sports scores or stock market updates. This module is called "sse" and can be used to implement server-sent events.

WebRTC

WebRTC is a real-time communication protocol that allows browsers to establish peer-to-peer connections. It provides a low-latency communication channel between clients without the need for a server. This "node-webrtc" can be used to implement WebRTC.

MQTT

MQTT is a lightweight messaging protocol well suited for IoT applications. It provides a publish/subscribe model for communication between clients and servers. This module is called "mqtt" and can be used to implement MQTT.

How to ensure communication security?

Security is paramount to any real-time communication application. This module called "crypto" can be used to secure communication between clients and servers. This module provides encryption and decryption functions to send encrypted information between client and server.

In addition, each type has a corresponding module, for example, WebSocket has a "ws" module, and the safe way is to wrap it with "https" instead of "http".

code example

Socket.IO — client server message example

Server side code:

// Install with 'npm i <module>' & Import necessary modules
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

io.on('connection', (socket) => {
  console.log('User connected');

  socket.on('chat:message', (data) => {
    io.emit('chat:message', data);
  });

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

Client code:

<!DOCTYPE html>
<html>
<head>
  <title>Socket.IO Chat</title>
</head>
<body>
  <div id="messages"></div>
  <form id="chat-form">
    <input type="text" id="message-input">
    <button type="submit">Send</button>
  </form>
  <script src="/socket.io/socket.io.js"></script>
  <script>
    const socket = io();
    const messagesDiv = document.getElementById('messages');
    const chatForm = document.getElementById('chat-form');
    const messageInput = document.getElementById('message-input');

    chatForm.addEventListener('submit', (event) => {
      event.preventDefault();
      const message = messageInput.value.trim();
      if (message) {
        socket.emit('chat:message', message);
        messageInput.value = '';
      }
    });

    socket.on('chat:message', (data) => {
      const messageDiv = document.createElement('div');
      messageDiv.innerText = data;
      messagesDiv.appendChild(messageDiv);
    });
  </script>
</body>
</html>

WebSockets — client server message example

Server side code:

// Install with 'npm i <module>' & Import necessary modules
const WebSocket = require('ws'); // use wss for secured option
const server = new WebSocket.Server({ port: 3000 });

server.on('connection', (socket) => {
  console.log('User connected');

// Handle socket connection
  socket.on('message', (message) => {
    server.clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });

  socket.on('close', () => {
    console.log('User disconnected');
  });
});

Client code:

<!DOCTYPE html>
<html>
<head>
  <title>WebSockets Chat</title>
</head>
<body>
  <div id="messages"></div>
  <form id="chat-form">
    <input type="text" id="message-input">
    <button type="submit">Send</button>
  </form>
<script>
    const socket = new WebSocket('ws://localhost:3000');
    const messagesDiv = document.getElementById('messages');
    const chatForm = document.getElementById('chat-form');
    const messageInput = document.getElementById('message-input');
    chatForm.addEventListener('submit', (event) => {
      event.preventDefault();
      const message = messageInput.value.trim();
      if (message) {
        socket.send(message);
        messageInput.value = '';
      }
    });
    socket.addEventListener('message', (event) => {
      const messageDiv = document.createElement('div');
      messageDiv.innerText = event.data;
      messagesDiv.appendChild(messageDiv);
    });
  </script>
</body>
</html>

Server Sent Events (SSE) - clock event example

Server side code:

The benefits of this article, free C++ audio and video learning materials package, technical video/code, including (audio and video development, interview questions, FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, codec, push-pull stream, srs)↓↓↓ ↓↓↓See below↓↓Click at the bottom of the article to get it for free↓↓

// Install with 'npm i <module>' & Import necessary modules
const express = require('express');
const app = express();

app.get('/events', (req, res) => {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive'
  });

  const interval = setInterval(() => {
    res.write(`data: ${new Date().toLocaleTimeString()}\n\n`);
  }, 1000);

  req.on('close', () => {
    clearInterval(interval);
    res.end();
  });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

Client code:

<!DOCTYPE html>
<html>
<head>
  <title>Server-Sent Events Clock</title>
</head>
<body>
  <div id="clock"></div>
<script>
    const source = new EventSource('/events');
    const clockDiv = document.getElementById('clock');
    
    source.addEventListener('message', (event) => {
      clockDiv.innerText = event.data;
    });
  </script>
</body>
</html>

WebRTC - video streaming example

Server side code:

// Install with 'npm i <module>' & Import necessary modules
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } = require('wrtc');

// Serve static files from public directory
app.use(express.static('public'));

io.on('connection', socket => {
  console.log('Client connected:', socket.id);
  let pc = new RTCPeerConnection();

  // Handle offer from client
  socket.on('offer', offer => {
    console.log('Received offer');

    pc.setRemoteDescription(new RTCSessionDescription(offer))
      .then(() => {
        return navigator.mediaDevices.getUserMedia({ audio: true, video: true });
      })
      .then(stream => {
        console.log('Got local stream');

        // Add local stream to peer connection
        stream.getTracks().forEach(track => {
          pc.addTrack(track, stream);
        });

        // Handle ice candidates
        pc.onicecandidate = event => {
          if (event.candidate) {
            socket.emit('candidate', event.candidate);
          }
        };

        // Handle remote stream
        pc.ontrack = event => {
          console.log('Received remote stream');
          socket.emit('answer', pc.localDescription);
        };

        // Create answer
        pc.createAnswer()
          .then(answer => {
            return pc.setLocalDescription(answer);
          })
          .catch(error => {
            console.log('Error creating answer:', error);
          });
      })
      .catch(error => {
        console.log('Error getting user media:', error);
      });
  });

  // Handle disconnect from client
  socket.on('disconnect', () => {
    console.log('Client disconnected:', socket.id);
    pc.close();
  });
});

const PORT = process.env.PORT || 3000;
http.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

Client code:

<html>
  <head>
    <meta charset="UTF-8">
    <title>WebRTC Example</title>
  </head>
  <body>
    <h1>WebRTC Example</h1>
    <div>
      <video id="localVideo" autoplay></video>
      <video id="remoteVideo" autoplay></video>
    </div>
    <div>
      <button id="callButton">Call</button>
      <button id="hangupButton" disabled>Hang Up</button>
    </div>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      const socket = io.connect('http://localhost:3000');
      const localVideo = document.getElementById('localVideo');
      const remoteVideo = document.getElementById('remoteVideo');
      const callButton = document.getElementById('callButton');
      const hangupButton = document.getElementById('hangupButton');
      
      let pc = new RTCPeerConnection();
      
      // Disable hang up button initially
      hangupButton.disabled = true;
      
      // Handle call button click
      callButton.onclick = () => {
        console.log('Calling');
      
        navigator.mediaDevices.getUserMedia({ audio: true, video: true })
          .then(stream => {
            console.log('Got local stream');
            localVideo.srcObject = stream;
      
            // Add local stream to peer connection
            stream.getTracks().forEach(track => {
              pc.addTrack(track, stream);
            });
      
            // Handle ice candidates
            pc.onicecandidate = event => {
              if (event.candidate) {
                socket.emit('candidate', event.candidate);
              }
            };
      
            // Handle remote stream
            pc.ontrack = event => {
              console.log('Received remote stream');
              remoteVideo.srcObject = event.streams[0];
            };
      
            // Create offer
            pc.createOffer()
              .then(offer => {
                return pc.setLocalDescription(offer);
              })
              .then(() => {
                socket.emit('offer', pc.localDescription);
              })
              .catch(error => {
                console.log('Error creating offer:', error);
              });
      
            // Enable hang up button
            hangupButton.disabled = false;
      
            // Handle hang up button click
            hangupButton.onclick = () => {
              console.log('Hanging up');
              pc.close();
              remoteVideo.srcObject = null;
              hangupButton.disabled = true;
              callButton.disabled = false;
            };
          })
          .catch(error => {
            console.log('Error getting user media:', error);
          });
      };
      
      // Handle answer from other user
      socket.on('answer', answer => {
        console.log('Received answer');
        pc.setRemoteDescription(new RTCSessionDescription(answer))
          .catch(error => {
            console.log('Error setting remote description:', error);
          });
      });
      
      // Handle ice candidate from other user
      socket.on('candidate', candidate => {
        console.log('Received candidate');
        pc.addIceCandidate(new RTCIceCandidate(candidate))
          .catch(error => {
            console.log('Error adding ice candidate:', error);
          });
      });
    </script>
  </body>
</html>

MQTT example

Publisher code:

// Install with 'npm i <module>' & Import necessary modules
const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://test.mosquitto.org');

client.on('connect', () => {
  console.log('connected to MQTT broker');

  setInterval(() => {
    client.publish('test', 'Hello MQTT');
  }, 1000);
});

client.on('error', (error) => {
  console.log(error);
});

Subscriber side code:

// Install with 'npm i <module>' & Import necessary modules
const mqtt = require('mqtt');
const client = mqtt.connect('mqtt://test.mosquitto.org');

client.on('connect', () => {
  console.log('connected to MQTT broker');

  client.subscribe('test', (error) => {
    if (error) {
      console.log(error);
    }
  });
});

client.on('message', (topic, message) => {
  console.log(`${topic}: ${message}`);
});

client.on('error', (error) => {
  console.log(error);
});

in conclusion

Node.js is an excellent choice for building real-time communication applications because of its speed, scalability, and reliability. Its event-driven architecture can handle multiple connections simultaneously, and the use of the V8 JavaScript engine ensures high performance. Building real-time communication applications with Node.js is easy with the help of libraries like Socket.IO.

However, when dealing with real-time communication applications, security is of paramount importance, so encryption must be used to secure communication between client and server.

In addition, Node.js also provides a variety of methods to achieve real-time data communication, each method has its own characteristics and advantages. Choosing the right method depends on the specific requirements of your application. Socket.IO and WebSockets are the most common real-time communication methods, while Server-Sent Events, WebRTC and MQTT are suitable for specific use cases.

In summary, Node.js is a powerful tool for building real-time communication applications and is worth considering for any project requiring real-time communication.

Original address: Why use Node.js for real-time communication? - Real-time interactive network

The benefits of this article, free C++ audio and video learning materials package, technical video/code, including (audio and video development, interview questions, FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, codec, push-pull stream, srs)↓↓↓ ↓↓↓See below↓↓Click at the bottom of the article to get it for free↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/131963416