Member-only story

Socket io in Nodejs or JS

habtesoft
2 min readMar 13, 2023

--

Socket.IO is a JavaScript library for real-time web applications. It allows real-time, bidirectional communication between clients and servers using WebSockets, long-polling, and other techniques. Socket.IO is available for use with Node.js and can be installed using the npm package manager.

Not a Medium member? Read this article here

To use Socket.IO with Node.js, follow these steps:

  1. Install Node.js on your machine if it is not already installed
  2. Create a new Node.js project and install the Socket.IO package using npm. You can do this by running the following command in your terminal:
npm init npm
install socket.io

3. Create a new JavaScript file and require the Socket.IO package:

const io = require('socket.io')();

4. Set up a connection event listener to listen for incoming socket connections:

io.on('connection', (socket) => {
console.log('a user connected');
});

5. Start the Socket.IO server by listening on a specific port:

io.listen(3000);

6. In your client-side HTML file, include the Socket.IO client library:

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

--

--

habtesoft
habtesoft

Written by habtesoft

Passionate JavaScript developer with a focus on backend technologies. Always eager to connect and learn. Let’s talk, https://buymeacoffee.com/habtesoftat

No responses yet