[Solved] What is difference between WebSockets and Socket.io?


Scenario: I’m new to JavaScript (have experience C/C++/Java), and working on a React UI with NodeJS server wherein the server will get updates on the displayed data at specific intervals (say 5 secs). The server is supposed to push the data to React UI to update the displayed data asap.

This is how I would do it.

Contenders: I came across 2 JS libraries Websockets (is lib a right term for websocket?) and Socket.io to achieve this. To me they both appear same.

In short, I am aware of two different ways of communicating between the server and client: WEBSOCKET and SERVER-SENT EVENTS (SSE). They are slightly different so it would be highly advisable to Google both options to find out which would suit you better.

Socket.io is essentially a library to assist with the creation/management of websockets. Imagine it to be your .NET Framework when you could have written a system yourself in C instead.

Did I Google?: Yes, but with my lack of knowledge of JS, I couldn’t understand the difference.

Here are some links that can help you:

SSE:

  1. https://www.w3schools.com/html/html5_serversentevents.asp

  2. https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

WEBSOCKET

  1. https://www.html5rocks.com/en/tutorials/websockets/basics/

  2. https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

COMPARISON

  1. https://www.google.com.au/search?safe=off&ei=MmNLWuGkLsHQ0AS87J_4Bw&q=websocket+vs+sse&oq=websocket+vs+sse

Problem: So can you please help me understand the difference between
these 2 libs (libs?) and help me decide which one is more suitable
here?

As I have mentioned before, you have technically only mentioned one package library (socket.io) which manages the websockets via their API. I would assume the other method would be to do it manually using the native JavaScript method. It is up to you which one you choose and you’ll surely find that there are advantages and disadvantages with both.

7

solved What is difference between WebSockets and Socket.io?