SoFunction
Updated on 2025-03-10

Building an instant communication system based on and

Use nodejs to build a websocket server

Not only can it build a client's websocket service, but it also supports nodejs server-side websocket.

Let me introduce how to install and configure nodejs.

Enter/#downloadDownload the msi file. Click next to install. Finally, the file will be automatically installed in the C:\nodejs directory.

After the installation is completed, the environment environment variables will be automatically configured. If there is no automatic configuration, add ;C:\nodejs\ to the path by yourself.

After the installation is completed, the modules that are managed by npm need to be configured.

Installing npm under window requires git to be installed.

After installing git, open gitbush. Perform the following steps:

git config --system  /bin/

git clone --recursive git:///isaacs/

cd npm

node  install npm -gf

The first is that there will be no prompts for setting up. The second step will go to github to download npm and will download the file and progress. The fourth step is to install npm to copy several files cmd files and mode_modules folder to the nodejs directory.

This way, npm is configured.

If you need to install any module, enter npm install *** directly.

If you don’t have npm or Windows users can use github to download and put it into the node_modules folder. For specific configuration, please refer to the article: "Nodejs Tutorial: Configured Windows Directory Structure"

nodejs installation

Use the node plug-in management package, run the following command to install successfully

npm install 

An example of implementation

Client code:

<html> 

<head> 

  <title></title> 

  <script src="../js/"></script> 

  <script type="text/javascript"> 

    function doit() { 

      var socket = ('http://localhost'); 

      ('news', function (data) {//Receive data called 'new' sent from the server
        ();//data is the data sent by the server.
        ('my new event', { my:'new data' });//Send data to the server to realize two-way data transmission
      }); 

      ('other', function (data) {//Receive another data named 'other',
        (); 

        ('event1', { my:'other data' }); 

      }); 

    } 

  </script> 

</head> 

<body> 

<button id='btn' onclick="doit()">click me</button> 

</body> 

</html> 

Can/LearnBoost/-clientDownload to locally and point to the native js library in <script src="..">.

Server implementation using nodejs

var http= require('http'), io= require(''), express= require('express'); 

var app = (), io = (app); 

(80); 

('connection', function (socket) { 

 ('news', { hello: 'world' });// Listen, once the client is connected, data is sent. The first parameter 'new' is the data name, and the second parameter is both data
 ('my other event', function (data) {//Capture the data sent by the client named 'my other event'
  (); 

 }); 

 ('other', { hello: 'other world' });//Send another data
 ('evnet1', function (data) {//Capture another data
  (); 

 }); 

}); 

Test results can be displayed normally by the client

The server side displays the results:

C:\java\Nodejs>node

Note: The code should be in the same directory as npm_module. Otherwise, there will be an error that the module cannot be found.