Professional Documents
Culture Documents
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
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.
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);
});
});