Socket.IO

Yehong Piao
Oct 19, 2020

What is 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. Like Node.js, it is event-driven.

It can be installed with the npm tool. -WikiPedia

Why use Socket.IO

What Socket.IO does is that it will build up a channel, so that all the users who access to the channel will get real time update. Imagine that you store data to the database, it is a single-directional communication other than bi-directional. It means that client need to keep pulling data from database to get updated information. This will take longer time, and inefficient.

Setting up Socket.IO

As mentioned before, Socket.IO need tow parts of libraries, server-side, and the client-side. For example, if you have Ruby backend, you can run Socket server in ruby, also you can run Socket server independently. For client-side, since the Socket.IO is one of the JavaScript library, you just need to use your frontend framework as Socket client-side library.

Socket-server

Ruby-server

React-frontend

How to use Socket.IO

Once you all set up with framework, you can use <socket.emit> to sending messages to all the users who access to specific URL. For example, you are running your Ruby on http://localhost:3000, Socket on http://localhost:4000, and your frontend on http://localhost:3001. Then all the users who access to http://localhost:3001 will get update information realtime.

--

--