Socket.io with node JS

Socket.io with node JS

Socket.io with node JS

Socket.IO is a real-time communication library between server and client. In Socket.IO, titles are shared only once and also work on the TCP layer. Web Sockets are the basis of Socket.IO library. Easy to use Socket.IO for explicit apps not built with express.it works on all platforms, browser or device, with equal focus on reliability and speed.

Socket.IO works primarily for event-based communications. Here the server or client emits the event and is hosted by another.

Firstly, install Express to your project using the following command in your node.js console.

npm install express –save

Next, install Socket.IO in your project using the following command in your node.js console

npm install socket.io

Implementation:

We need http module in node.js to work with express and Socket.IO as it will handle our server. The dynamic server is a retrieval of the createServer method (which gets the express app as as the first parameter) and, the server variable will be used as the first parameter in the socket.io module listen method.

Create a file (named by server.js) wherever you want and add the following code to it:

Note: in order to start the server itself, the listen method needs to be done from a server variable, not the app itself. You need to understand this to prevent known errors such as "SocketIO: err_connection_refused".

Put the following code in the the server.js file:

As you can see if there is any connection request from any browser, device or tool we are printing “socket connected with ID=” with socket ID message in console.

Note: The listening port for our server is 3000.

Now, we need to run the following command to run the Socket.IO using node js:

node server,js

After successfully running of server now you can check if the Socket.Io is working fine or not by hitting connection request from browser, tool or device. I am using postman to sending connection request to server.

As you can see in the screenshot Socket connected successfully with listening port 3000.

I am using localhost that’s why socket connected to localhost with port 3000. If you are using live server, you can use your IP address instead of localhost. Also, you can check console as we are printing message if socket connected successfully.

Please check the screenshot below:

Conclusion:

We have learnt that how Socket.Io get installed with node.js and express. We can also find the how socket get connected with any tool. Basically, Socket.io is much better than the WebSocket. Many of the limitation of WebSocket has been overcomes by the Socket.Io. Socket.Io Actually works in real time. For more information you can check official documentation of Socket.Io.

I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.

-

Our Offices

INDIA

F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163

USA

13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079

CANADA

15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147