nodejs
  1. nodejs-raspi-websocket

Node.js Raspberry Pi WebSocket

WebSocket is a protocol that provides full-duplex communication channels over a single TCP connection. It is used for web applications that require real-time communication between a client and a server. In this tutorial, we will learn how to use WebSocket with Raspberry Pi and Node.js.

Syntax

The following syntax is used to create a WebSocket server in Node.js:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

Here we import the WebSocket module and create a WebSocket server on port 8080.

Example

Let's say we have a Raspberry Pi connected to a temperature sensor and we want to send the temperature readings to a client web application in real-time using WebSocket. Here's how we can do it:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
  console.log('WebSocket connection established');
  
  setInterval(() => {
    // code to read temperature from the sensor
    const temperature = Math.random() * 100;
    ws.send(temperature.toString());
  }, 1000);

  ws.on('close', function close() {
    console.log('WebSocket connection closed');
  });
});

In this example, we create a WebSocket server on port 8080 and log a message when a connection is established. We then create a setInterval function to read the temperature from the sensor and send it to the connected client every 1 second. Finally, we log a message when the WebSocket connection is closed.

Output

The output of this example is the temperature readings that are sent to the client web application in real-time.

Explanation

In the example above, we create a WebSocket server using the WebSocket module in Node.js. When a client connects to the WebSocket server, we log a message to the console. We then start a setInterval function that reads the temperature sensor and sends the temperature readings to the client every 1 second. When the WebSocket connection is closed, we log a message to the console.

Use

WebSocket is useful for real-time communication between a client and a server. It can be used for applications where data needs to be transmitted quickly and efficiently, such as online gaming, chat applications, and IoT devices.

Important Points

  • When using WebSocket, make sure to handle errors and close events appropriately.
  • WebSocket connections should always be secured with SSL/TLS to prevent attacks like MITM.
  • WebSocket connections should be kept open as long as possible to reduce the overhead of opening and closing the connections.

Summary

In this tutorial, we learned how to use WebSocket with Raspberry Pi and Node.js. We covered the syntax, example, output, explanation, use, and important points of using WebSocket with Raspberry Pi and Node.js. With this knowledge, you can now create real-time communication channels between a client and server using the WebSocket protocol in Node.js.

Published on: