Home  >  Article  >  Operation and Maintenance  >  How to use WebSocket technology in Linux

How to use WebSocket technology in Linux

WBOY
WBOYOriginal
2023-06-18 19:38:503454browse

With the increase in modern network applications, WebSocket technology has become very popular. It is a long connection technology based on the TCP protocol that can create a two-way communication pipeline between the client and the server. In this article, we will explain how to create a simple real-time chat application using WebSocket technology in Linux systems.

1. Install Node.js

To use WebSocket, you first need to install Node.js in the Linux system. Node.js is a server-side JavaScript runtime environment that helps us write efficient real-time web applications. The following is the command to install Node.js on Debian/Ubuntu:

$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo apt-get install npm

2. Create a WebSocket server

Next we create a simple WebSocket server. Create a file named server.js in the editor and copy the following code into it:

const WebSocket = require('ws');

const server = new WebSocket.Server({ port: 8080 });

server.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
    server.clients.forEach(function each(client) {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message);
      }
    });
  });
});

In this example, we used the built-in WebSocket module of Node.js and then created a WebSocket server (Port is 8080). Whenever a new client connects to the server, we will create a WebSocket instance for that client and listen for its messages on the server. Whenever a message is received, the server broadcasts the message to each client.

3. Create a client

Using WebSocket technology, we can achieve two-way communication between the client and the server. In this example, we will use a command line tool called ws to create a WebSocket client. The following is the command to install ws on a Linux system:

$ sudo npm install -g ws

Create a file named client.js in the editor and copy the following code into it:

const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:8080');

ws.on('open', function open() {
  console.log('connected');
});

ws.on('message', function incoming(data) {
  console.log(data);
});

process.stdin.on('data', function incoming(data) {
  ws.send(data.toString().trim());
});

In this example , we used the ws module to create a WebSocket client and connected it to the WebSocket server we created earlier. Whenever a connection is established, we will output connected on the console. Any messages from the server will be printed to the console. Finally, we set up a stdin listener that when the client enters some data, it will be immediately sent to the server.

4. Test the application

Open two windows on the terminal, one for the server and the other for the client. Run the following command in the server window:

$ node server.js

Run the following command in the client window:

$ node client.js

Now you can enter some messages in the client window and see it in the server window They are broadcast to every client. Vice versa, if you enter some data in the server window, it will be displayed on every client.

5. Summary

Using WebSocket technology on Linux systems can help us create applications with real-time two-way communication capabilities. In this article, we create a simple live chat application by using Node.js and ws module. Hope this article is helpful to beginners!

The above is the detailed content of How to use WebSocket technology in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn