পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Showing posts with label Socket.IO Tutorial. Show all posts
Showing posts with label Socket.IO Tutorial. Show all posts

Wednesday, February 8, 2017

Socket.IO - Overview

Socket.IO

Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for node.js. Both components have a nearly identical API.

Socket.IO - Environment

To get started with developing using the Socket.IO, you need to have Node and npm(node package manager) installed. If you don’t already have these, head over to Node setup to install node on your local system. Confirm that node and npm are installed by running the following commands in your terminal.

Socket.IO - Hello World

Create a file called app.js and enter the following to set up an express application:
var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

Socket.IO - Event Handling

Sockets work based on events. There are some reserved events that can be accessed using the socket object on the server side: connect, message, disconnect, reconnect, ping, join and leave. The client side socket object also provides us with some reserved events: connect, connect_error, connect_timeout, reconnect, etc.

Socket.IO - Broadcasting

Broadcasting means sending a message to all connected clients. Broadcasting can be done at multiple levels. We can send the message to all connected clients, to clients on a namespace and clients in a particular room. The latter 2 would be covered in their respective chapters.

Socket.IO - 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 (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels.

Socket.IO - Rooms

Within each namespace, you can also define arbitrary channels that sockets can join and leave. These channels are called rooms. Rooms are used to further separate concerns.
Rooms also share the same socket connection like namespaces.

Socket.IO - Error Handling

We've worked on local servers till now which will almost never give us errors related to connections, timeouts, etc. But in real life production environments, handling such errors are of utmost importance. So we'll now discuss how we can handle connection errors on the client side.

Socket.IO - Logging and Debugging

Socket.IO uses a very famous debugging module developed by ExpresJS's main author, called debug.
Earlier socket.IO used to log everything to the console making it quite difficult to actually debug the problem. After the v1.0 release, you can specify what you want to log.

Socket.IO - Internals

Fallbacks

Socket.IO has a lot of underlying transport mechanisms, which deal with various constraints arising due to cross browser issues, WebSocket implementations, firewalls, port blocking, etc.
Though W3C has a defined specification for WebSocket API, it is still lacking in implementation.

Socket.IO - Chat Application

Now that we are well aquainted with Socket.IO, let us write a chat application which we can use to chat on different chat rooms. We will allow users to choose a username and allow them to chat using them. So first let us set up our HTML file to request for a username:

Socket.IO - Quick Guide


Socket.IO

Socket.IO is a JavaScript library for realtime web applications. It enables realtime, bi-directional communication between web clients and servers. It has two parts: a client-side library that runs in the browser, and a server-side library for node.js. Both components have a nearly identical API.

Socket.IO - Useful Resources

The following resources contain additional information on Socket.IO. Please use them to get more in-depth knowledge on this topic.

Socket.IO - Discussion

Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed. Socket.IO is built on top of the WebSockets API(Client side) and Node.js. It is one of the most depended-upon library on npm.