Getting Started with Socket.IO: A Step-by-Step Tutorial

Rind Devran Tukan
Rind Devran Tukan
< 1 min read
Socket.IO tutorial setup
Reading Time: < 1 minute

Setting Up Socket.IO

This guide walks you through installing and configuring Socket.IO in a Node.js environment.

Installation

npm install socket.io express

Basic Server Setup

const express = require('express');
const http = require('http');
const socketIO = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = socketIO(server);

io.on('connection', (socket) => {
  console.log('A user connected');
  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

Client-Side Integration

<script src='/socket.io/socket.io.js'></script>
<script>
  const socket = io();
  socket.on('connect', () => {
    console.log('Connected to server');
  });
</script>

Conclusion

You now have a basic Socket.IO server and client ready for real-time interactions.

Related Articles

Discover more stories from our blog