100% found this document useful (1 vote)
543 views5 pages

Socket Io

Socket.IO is a JavaScript library that enables real-time bidirectional event-based communication between web clients and servers. It uses WebSockets for browser clients that support it, and falls back to other technologies like long-polling where necessary. Socket.IO allows broadcasting messages to all connected clients and supports custom events with parameters. It is commonly used for building real-time applications like chat, games, and dashboards.

Uploaded by

Chirag Purohit
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
100% found this document useful (1 vote)
543 views5 pages

Socket Io

Socket.IO is a JavaScript library that enables real-time bidirectional event-based communication between web clients and servers. It uses WebSockets for browser clients that support it, and falls back to other technologies like long-polling where necessary. Socket.IO allows broadcasting messages to all connected clients and supports custom events with parameters. It is commonly used for building real-time applications like chat, games, and dashboards.

Uploaded by

Chirag Purohit
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/ 5

Node.Js Socket.

io
What is Websocket?
WebSocket is a protocol providing full-duplex (both server
and client can send message to each other)
communications channels over a single TCP connection.

What is Socket.Io?
Socket.IO is a WebSocket API. Socket.IO provides an
event-oriented API that works across all networks,
devices and browsers. Its incredibly robust and highly
performant, which is very suitable for multiplayer games
or realtime communication. Socket.io is not only
dependent on websocket. Socket.io is using websocket
when it is available otherwise it will make connection by
long-polling method(XHR-transport). For example, if
the URL is http://localhost/users, a transport connection
will be established to http://localhost and a Socket.IO
connection will be established to /users.

What is XHR-transport?
XHR-transport is long-lived HTTP technique used as a
long-polling alternative to TCP when TCP is difficult or
impossible to employ directly. If the server does not
have any information available for the client when the
poll is received, instead of sending an empty response,
the server holds the request open and waits for response
information to become available. Once it does, the
server immediately sends an HTTP/S response to the
client, completing the open HTTP/S Request

Main Parts of Socket.Io

Server The URL on which client will


connect(www.example.com)
Port on which all the events will listen by Socket.Io
NameSpaces
Events

Namespaces :
Socket.IO allows you to namespace your sockets, which
essentially means assigning different endpoints or paths.
This is a useful feature to minimize the number of
resources and at the same time separate concerns within
your application by introducing separation between
communication channels.

Default namespace
We call the default namespace / and its the one
Socket.IO clients connect to by default, and the one the
server listens to by default.

Events :
The main idea behind Socket.IO is that you can send and
receive any events you want, with any data you
want. Following are some pre-defined events.

connect : Fired upon connecting.

error : Fired upon a connection error


Parameters:

Object error data


disconnect : Fired upon a disconnection.
reconnect : Fired upon a successful reconnection.
Parameters:

Number reconnection attempt number


reconnect_attempt : Fired upon an attempt to
reconnect.
reconnecting : Fired upon an attempt to reconnect.
Parameters:

Number reconnection attempt number


reconnect_error : Fired upon a reconnection
attempt error.
Parameters:

Object error object


reconnect_failed : Fired when couldnt reconnect
within reconnectionAttempts

How to use Socket.io?


Socket.IO has a specified protocol on the top of
WebSocket, so you have to use a client for Socket.IO, not
for WebSocket. In the case of Socket.Io a message from
server will reach on all clients.
There are some Socket.IO clients in Java and Android, but
you will find socket.io-java-client is the best way to go.
https://github.com/Gottox/socket.io-java-client

Sample Usage
An example to send and receive events. The following is a
code of a simple echo server in Node.
io.sockets.on('connection', function(socket) {
socket.on('echo', function(data) {
socket.emit('echo back', data);
});
});

The next is a Java code to connect to the server from


Android. The usage of the client is similar to the Socket.IO
client for JavaScript. You can send a message
via SocketIO#emit or SocketIO#send method, and can
receive data
via IOCallback#on and IOCallback#onMessage callbacks.
SocketIO socket = new SocketIO("http://127.0.0.1:3001");
socket.connect(new IOCallback() {
@Override
public void on(String event, IOAcknowledge ack, Object... args) {
if ("echo back".equals(event) && args.length > 0) {
Log.d("SocketIO", "" + args[0]);
// -> "hello"
}
}
@Override
public void onMessage(JSONObject json, IOAcknowledge ack) {}
@Override
public void onMessage(String data, IOAcknowledge ack) {}
@Override
public void onError(SocketIOException socketIOException) {}
@Override
public void onDisconnect() {}
@Override
public void onConnect() {}
});
socket.emit("echo", "hello");

What is the difference between XMPP and


Node.Js (Socket.Io)?
In XMPP there is a one to one connection for all clients
while in Node.js only one connection is made for all
clients and all messages are delivered to each and every
clients.
XMPP uses XML language for data transfer while Node.js
uses Json fomat and String format for data transfer.
Node.js are written in a commonly understood language
(Javascript) rather than XMPP servers which the common
ones are written in Java which aren't so widely
understood. If you want to have full control over the
server behaviour and write clever modules then I suspect
that node will be the best solution for you.

You might also like