0% found this document useful (0 votes)
19 views

Socket

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Socket

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Socket.

IO is a JavaScript library that enables real-time, bidirectional, and


event-based communication between web clients and servers. It's
particularly useful for applications that require real-time updates, such as
chat applications, live notifications, or collaborative tools.
Key Features of Socket.IO:
1. Real-Time Communication: Enables real-time data transfer
between the server and clients, which is essential for applications
like chat rooms, live feeds, and online gaming.
2. Cross-Browser Compatibility: Works across all modern browsers
and even in environments that don't support WebSockets natively
by falling back to other technologies like long-polling.
3. Event-Driven: Socket.IO is built on an event-driven architecture,
making it easy to listen for and emit custom events.
4. Automatic Reconnection: If the connection between the client
and server is lost, Socket.IO automatically tries to reconnect,
ensuring stable communication.
5. Namespaces and Rooms: These features allow you to split the
logic into different channels or groups, making it easier to manage
communication in complex applications.

Basic Usage
1. Installation
To get started, you need to install the Socket.IO server and client libraries.
 On the server:
npm install socket.io
On the client:

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

Setting Up a Basic Server


Here’s a simple example of setting up a Node.js server with Socket.IO:
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();


const server = http.createServer(app);
const io = new Server(server);

io.on('connection', (socket) => {


console.log('a user connected');

socket.on('disconnect', () => {
console.log('user disconnected');
});

socket.on('chat message', (msg) => {


console.log('message: ' + msg);
io.emit('chat message', msg); // Broadcast the message to everyone
});
});

server.listen(3000, () => {
console.log('listening on *:3000');
});

Client-Side Code
The client code can connect to the server and send/receive messages:

<!DOCTYPE html>
<html>
<head>
<title>Socket.IO Chat</title>
</head>
<body>
<input id="messageInput" type="text" placeholder="Type a
message...">
<button onclick="sendMessage()">Send</button>
<ul id="messages"></ul>

<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();

socket.on('chat message', (msg) => {


const li = document.createElement('li');
li.textContent = msg;
document.getElementById('messages').appendChild(li);
});

function sendMessage() {
const msg = document.getElementById('messageInput').value;
socket.emit('chat message', msg);
document.getElementById('messageInput').value = '';
}
</script>
</body>
</html>

Concepts
 Namespaces: Useful for splitting up the logic in different parts of
an application. For example, /admin could be a separate namespace
for administrative functionality.
 Rooms: Allow clients to join specific channels. Messages can be
broadcast to all clients in a room, rather than all connected clients.

You might also like