Based on socket. IO and node.js to build instant messaging system

  • 2020-03-30 03:37:28
  • OfStack

Use socket.io and nodejs to build websocket server

Let me show you how to install and configure nodejs.

Go to (link: http://nodejs.org/#download) to download the msi file. Always click next to install. The final file will be automatically installed in the C:\nodejs directory.

When the installation is complete, the environment environment variables are automatically configured. If there is no automatic configuration, add it to the path manually. C: \ nodejs \.

Once the installation is complete, you need to configure the NPM to manage the modules for node.js.

Installing NPM under window requires you to install git.

After installing git, open gitbush. Follow these steps:


git config --system http.sslcainfo /bin/curl-ca-bundle.crt

git clone --recursive git://github.com/isaacs/npm.git

cd npm

node cli.js install npm -gf

The first one is that there will be no prompt, the second step is to download the NPM on github, and the second step is to download the files and progress. The fourth step is to install NPM to node.js.

This will configure the NPM.

If you need to install any module directly enter NPM install ***.

No NPM or Windows users can use github to download socket.io and put it into the node_modules folder. For specific configuration, please refer to the article "nodejs tutorial: configuring nodejs.exe's Windows directory structure".

Nodejs install socket. IO

Using the node plug-in to manage the package, run the following command to install socket.io successfully


npm install socket.io

An example of the socket.io implementation

Client code:


<html> 

<head> 

  <title></title> 

  <script src="../js/socket.io.client.js"></script> 

  <script type="text/javascript"> 

    function doit() { 

      var socket = io.connect('http://localhost'); 

      socket.on('news', function (data) {//Receive data called 'new' from the server

        console.log(data.hello);//Data is the data sent to the server.

        socket.emit('my new event', { my:'new data' });//Send data to the server to achieve two-way data transmission

      }); 

      socket.on('other', function (data) {//Receive another data called 'other',

        console.log(data.hello); 

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

      }); 

    } 

  </script> 

</head> 

<body> 

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

</body> 

</html> 

Socket. IO. Client. Js can download to a local (link: https://github.com/LearnBoost/socket.io-client), in < Script SRC = ".." > Point to the native js library.

The server is implemented with nodejs

Server2. Js


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

var app = express.createServer(), io = io.listen(app); 

app.listen(80); 

io.sockets.on('connection', function (socket) { 

 socket.emit('news', { hello: 'world' });//Listen, once the client is connected, that is, send data, the first parameter 'new' is the data name, the second parameter is both data

 socket.on('my other event', function (data) {//The capture client sends data called 'my other event'

  console.log(data.my); 

 }); 

 socket.emit('other', { hello: 'other world' });//Send another data

 socket.on('evnet1', function (data) {//Capture another data

  console.log(data.my); 

 }); 

}); 

Test results, the client can be normal display

Server-side display results:

C: \ Java \ Nodejs> The node server2. Js

Note: the code should be in the same directory as the npm_module. Otherwise there will be an error where the socket.io module cannot be found.


Related articles: