What is Socket.IO & How to use this?

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.



How does it Works?



The client will try to establish a WebSocket connection. WebSocket is a Communication protocol which provides a full-duplex and low-latency channel between the server and the browser. Over 97%  of the browser in 2021 supports WebSocket.

WebSockets




Do you Know? There is an inbuild WebSocket class in your browser from which you can create real-time connections with your server.


So Why to use socket.io?

Because Socket.io provides more great features over plan WebSockets.


Features:

*Acknowledgments

*Namespaces

*Automatic reconnection

*Broadcasting to all clients or to a specific Room

*reliability (fallback to HTTP long-polling in case the WebSocket connection cannot be established)

*And much more



Simple Usage


Create a real-time connection with client and server.


Const app = require("express")();

Const httpServer = require("http").createServer(app);

const options = { /* .... */ };

const io = require{"socket.io"}(httpServer, options);

io.on("connection", socket => { /* .... */ });

httpServer.listen(5000);

Socket init with Express Js server.


Simple Usage


Client-side code to connect to connect with the socket.


import { io } from "socket.io-client";

const socket = io("https://server-domain.com");

socket.on("connect" , () => {

console.log("Connected with id", socket.id);

});


You can also use client library from a cdn


Project Ideas 


*Real-time Analytics

*Document collaboration 

*Real-time Games

*Chat Application




Other Languages?


There are also several client implementation in other languages, which are maintained by the community like python, 

* Java                   

*Swift

*Rust

*C++

* Dart

*.Net


Other server implementation :  *Java   *Python  * Go Lang


Ask any Doubt related to this site...

Post a Comment

Ask any Doubt related to this site...

Post a Comment (0)

Previous Post Next Post