0% found this document useful (0 votes)
15 views9 pages

Socket IO

The document provides a comprehensive list of Socket.IO methods, differentiating between server-side and client-side usage, along with brief descriptions of their functions. It covers all primary methods for both Socket.IO v4 and v5, detailing their purposes, syntax, and examples. The methods include event listeners, message sending, room management, and connection handling for both server and client contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Socket IO

The document provides a comprehensive list of Socket.IO methods, differentiating between server-side and client-side usage, along with brief descriptions of their functions. It covers all primary methods for both Socket.IO v4 and v5, detailing their purposes, syntax, and examples. The methods include event listeners, message sending, room management, and connection handling for both server and client contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/11/24, 3:30 PM ChatGPT

i need list of all Socket.io methods i said all that differenciate


serverside and clientside differenciate all only methods and what it will
do in --> i dont need code i said i need alll methods

Here’s a comprehensive list of Socket.IO methods, organized by server-side and client-side usage,
along with brief descriptions of what each method does. I'll cover all primary methods (for both
Socket.IO v4 and v5 versions), differentiating server-side from client-side.

Server-Side Methods (Node.js / Express with Socket.IO)


1. io.on(event, callback)

Registers an event listener on the server for a specific event, commonly used for listening
to "connection" events.

2. socket.on(event, callback)

Attaches a listener for a specific event on an individual socket (client connection).

3. socket.emit(event, data)

Sends a message to the client that is connected to this specific socket.

4. socket.broadcast.emit(event, data)

Sends a message to all connected clients except the one that triggered the event.

5. io.emit(event, data)

Sends a message to all connected clients.

6. socket.join(room)

Adds the socket to a specific room, allowing you to manage groups of clients.

7. socket.leave(room)

Removes the socket from a specific room.

8. io.to(room).emit(event, data)

Sends a message to all clients in a specific room.

9. socket.disconnect()

Disconnects the socket from the server.

10. socket.rooms

Returns a list of all rooms that the current socket is a part of.

11. io.sockets.sockets

Returns an object of all connected sockets.

12. io.sockets.adapter

Accesses the adapter interface, which allows inter-room communication and advanced
configurations.

13. io.socketsJoin(room)

Joins all connected sockets to a specific room.

14. io.socketsLeave(room)

Removes all sockets from a specific room.

15. socket.in(room).emit(event, data)

Emits an event only to clients in a specified room.

16. io.engine

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 1/9
11/11/24, 3:30 PM ChatGPT

Provides access to low-level Engine.IO information, such as ping intervals and timeouts.

17. socket.compress(compression)

Sets compression settings for emitted events. By default, messages are compressed.

18. io.close()

Closes the server and disconnects all clients.

Client-Side Methods (JavaScript / HTML with Socket.IO)


1. socket.on(event, callback)

Registers a listener for an event on the client side, like listening for messages from the
server.

2. socket.emit(event, data)

Sends a message to the server for a specific event.

3. socket.disconnect()

Manually disconnects the client from the server.

4. socket.connect()

Manually connects the client to the server.

5. socket.id

Retrieves the unique ID of the client’s socket connection.

6. socket.io

Gives access to the manager object, which holds connection settings and info.

7. socket.compress(compression)

Configures message compression for events sent from the client.

8. socket.open()

Alias for socket.connect() , connects the client if it was previously disconnected.

9. socket.volatile.emit(event, data)

Sends a message that does not guarantee delivery. Useful for less important data that does
not need reliability.

10. socket.connected

A boolean that returns true if the client is connected, false otherwise.

This list covers all standard methods used to establish, manage, and control connections and events
in Socket.IO.

now start explaining each and every method that you given and
explain with full Syntax and a small example ok

Below is a detailed explanation of each Socket.IO method on both the server side and client side,
including syntax and example usage for each.

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 2/9
11/11/24, 3:30 PM ChatGPT

Server-Side Methods (Node.js / Express with Socket.IO)


1. io.on(event, callback)

Purpose: Registers an event listener on the server for specific events.

Syntax:

javascript Copy code

io.on('event', (socket) => { /* callback code */ });

Example:

javascript Copy code

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

2. socket.on(event, callback)

Purpose: Attaches a listener for a specific event on an individual socket (client connection).

Syntax:

javascript Copy code

socket.on('event', (data) => { /* callback code */ });

Example:

javascript Copy code

socket.on('message', (msg) => { console.log('Received message:', msg); });

3. socket.emit(event, data)

Purpose: Sends a message to the connected client.

Syntax:

javascript Copy code

socket.emit('event', data);

Example:

javascript Copy code

socket.emit('welcome', 'Hello, client!');

4. socket.broadcast.emit(event, data)

Purpose: Sends a message to all connected clients except the sender.

Syntax:

javascript Copy code

socket.broadcast.emit('event', data);

Example:

javascript Copy code

socket.broadcast.emit('message', 'A new client has joined the chat');

5. io.emit(event, data)

Purpose: Sends a message to all connected clients.

Syntax:

javascript Copy code

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 3/9
11/11/24, 3:30 PM ChatGPT

io.emit('event', data);

Example:

javascript Copy code

io.emit('announcement', 'Server maintenance starts soon.');

6. socket.join(room)

Purpose: Adds the socket to a specified room.

Syntax:

javascript Copy code

socket.join('room');

Example:

javascript Copy code

socket.join('gameRoom');

7. socket.leave(room)

Purpose: Removes the socket from a specified room.

Syntax:

javascript Copy code

socket.leave('room');

Example:

javascript Copy code

socket.leave('gameRoom');

8. io.to(room).emit(event, data)

Purpose: Sends a message to all clients in a specific room.

Syntax:

javascript Copy code

io.to('room').emit('event', data);

Example:

javascript Copy code

io.to('gameRoom').emit('scoreUpdate', { score: 100 });

9. socket.disconnect()

Purpose: Disconnects the socket from the server.

Syntax:

javascript Copy code

socket.disconnect();

Example:

javascript Copy code

socket.disconnect();

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 4/9
11/11/24, 3:30 PM ChatGPT

10. socket.rooms

Purpose: Returns a list of all rooms the current socket is part of.

Syntax:

javascript Copy code

const rooms = socket.rooms;

Example:

javascript Copy code

console.log('Socket rooms:', socket.rooms);

11. io.sockets.sockets

Purpose: Provides access to all connected sockets.

Syntax:

javascript Copy code

io.sockets.sockets;

Example:

javascript Copy code

console.log(io.sockets.sockets);

12. io.sockets.adapter

Purpose: Accesses the adapter interface for inter-room communication and configuration.

Syntax:

javascript Copy code

io.sockets.adapter;

Example:

javascript Copy code

console.log(io.sockets.adapter.rooms);

13. io.socketsJoin(room)

Purpose: Joins all connected sockets to a specific room.

Syntax:

javascript Copy code

io.socketsJoin('room');

Example:

javascript Copy code

io.socketsJoin('broadcastRoom');

14. io.socketsLeave(room)

Purpose: Removes all sockets from a specific room.

Syntax:

javascript Copy code

io.socketsLeave('room');

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 5/9
11/11/24, 3:30 PM ChatGPT

Example:

javascript Copy code

io.socketsLeave('broadcastRoom');

15. socket.in(room).emit(event, data)

Purpose: Emits an event only to clients in a specified room.

Syntax:

javascript Copy code

socket.in('room').emit('event', data);

Example:

javascript Copy code

socket.in('gameRoom').emit('scoreUpdate', { score: 50 });

16. io.engine

Purpose: Provides access to low-level Engine.IO information.

Syntax:

javascript Copy code

io.engine;

Example:

javascript Copy code

console.log(io.engine.clientsCount);

17. socket.compress(compression)

Purpose: Sets compression settings for emitted events.

Syntax:

javascript Copy code

socket.compress(false).emit('event', data);

Example:

javascript Copy code

socket.compress(true).emit('largeData', { bigData });

18. io.close()

Purpose: Closes the server and disconnects all clients.

Syntax:

javascript Copy code

io.close();

Example:

javascript Copy code

io.close();

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 6/9
11/11/24, 3:30 PM ChatGPT

Client-Side Methods (JavaScript / HTML with Socket.IO)


1. socket.on(event, callback)

Purpose: Registers a listener for an event on the client side.

Syntax:

javascript Copy code

socket.on('event', (data) => { /* callback code */ });

Example:

javascript Copy code

socket.on('message', (msg) => { console.log('Received message from server:',


msg); });

2. socket.emit(event, data)

Purpose: Sends a message to the server for a specific event.

Syntax:

javascript Copy code

socket.emit('event', data);

Example:

javascript Copy code

socket.emit('joinRoom', 'gameRoom');

3. socket.disconnect()

Purpose: Manually disconnects the client from the server.

Syntax:

javascript Copy code

socket.disconnect();

Example:

javascript Copy code

socket.disconnect();

4. socket.connect()

Purpose: Manually connects the client to the server.

Syntax:

javascript Copy code

socket.connect();

Example:

javascript Copy code

socket.connect();

5. socket.id

Purpose: Retrieves the unique ID of the client’s socket connection.

Syntax:

javascript

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 7/9
11/11/24, 3:30 PM ChatGPT

Copy code
const socketId = socket.id;

Example:

javascript Copy code

console.log('My socket ID:', socket.id);

6. socket.io

Purpose: Provides access to connection settings and manager object information.

Syntax:

javascript Copy code

socket.io;

Example:

javascript Copy code

console.log(socket.io.engine);

7. socket.compress(compression)

Purpose: Configures message compression for events sent from the client.

Syntax:

javascript Copy code

socket.compress(false).emit('event', data);

Example:

javascript Copy code

socket.compress(true).emit('bigData', { data: 'lotsOfData' });

8. socket.open()

Purpose: Alias for socket.connect() , to reconnect the client.

Syntax:

javascript Copy code

socket.open();

Example:

javascript Copy code

socket.open();

9. socket.volatile.emit(event, data)

Purpose: Sends a message that does not guarantee delivery.

Syntax:

javascript Copy code

socket.volatile.emit('event', data);

Example:

javascript Copy code

socket.volatile.emit('ping', 'Check connection');

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 8/9
11/11/24, 3:30 PM ChatGPT

10. socket.connected

Purpose: Returns true if the client is connected, false otherwise.

Syntax:

javascript Copy code

const isConnected = socket.connected;

Example:

javascript Copy code

if (socket.connected) console.log('Client is connected');

This provides a full breakdown with syntax and small examples for each Socket.IO method on both
the server and client sides.

https://chatgpt.com/c/6731d441-c864-8006-9ff0-a46025c61ab4 9/9

You might also like