Socket.io v4 or websockets in 2023 - which should you use? - socket.io #1

Good Morning Developers
14 Jun 202310:52

Summary

TLDRIn this video, the instructor discusses the significance of Socket.IO and WebSockets within the context of a programming course, particularly focusing on the rationale behind emphasizing Socket.IO over WebSockets, even in 2023. The instructor introduces Socket.IO as a library that facilitates low latency, bi-directional, and event-based communication between clients and servers, contrasting it with the traditional HTTP request-response model. The discussion includes the benefits of Socket.IO, such as automatic reconnection, packet buffering, and multiplexing, which are not inherently supported by WebSockets. The instructor concludes by affirming Socket.IO's continued relevance and efficiency in implementing WebSockets, emphasizing its comprehensive features that still make it the preferable choice for real-time applications.

Takeaways

  • 💻 The video introduces the concepts of Socket.IO and WebSockets, aiming to clarify their roles and significance in real-time web communication.
  • 🔥 Socket.IO is emphasized as the primary focus of the course, even in 2023, highlighting its importance over native WebSockets for certain applications.
  • ⏰ If it's not morning where viewers are, the narrator humorously notes it's morning somewhere, setting a light-hearted tone for learning about Socket.IO.
  • 📰 Socket.IO facilitates low latency, bi-directional, and event-based communication between clients and servers, differing from traditional HTTP request-response cycles.
  • ✍️ WebSockets provide a two-way communication channel that remains open for real-time updates, a capability also supported by Socket.IO but with additional features.
  • 📊 Socket.IO builds upon WebSockets, offering solutions for real-world issues such as connection reliability and data packet management.
  • ⚠️ The video explains the historical necessity of Socket.IO as a layer over WebSockets to handle inconsistencies and limitations in early WebSocket implementations.
  • 📚 Through examples, the video demonstrates Socket.IO's additional features like automatic reconnection, event acknowledgments, and room-based broadcasting which are not natively supported by WebSockets.
  • 🔍 Socket.IO's relevance in 2023 is justified by its abstraction layer, providing robust solutions to common WebSocket challenges without requiring developers to reinvent the wheel.
  • ✅ The course promises to cover both Socket.IO and WebSockets, comparing them directly to showcase why Socket.IO remains the preferred choice for real-time web applications.

Q & A

  • What is the main purpose of this video?

    -The main purpose of this video is to explain the relationship between Socket.IO and WebSockets, and to justify why the course will primarily focus on Socket.IO over plain WebSockets.

  • What is the key difference between HTTP and Socket.IO/WebSockets?

    -HTTP is a request-response protocol where connections are terminated after each request, while Socket.IO and WebSockets establish a persistent, bi-directional connection between the client and server, enabling real-time communication without the need to re-establish connections.

  • Is the WebSocket API a native technology in JavaScript?

    -Yes, the WebSocket API is a native technology in JavaScript, meaning it is built into the language and does not require any additional libraries or frameworks to be used.

  • Why was Socket.IO created initially?

    -Socket.IO was created in 2010 to bring order and consistency to the chaos surrounding WebSockets at the time when browser support for WebSockets was in its infancy and often unreliable.

  • What are some key features that Socket.IO provides over plain WebSockets?

    -Some key features that Socket.IO provides over plain WebSockets include HTTP long-polling fallback, automatic reconnection, packet buffering, acknowledgments, broadcasting, and multiplexing.

  • Is Socket.IO still needed today, given the widespread support for WebSockets?

    -According to the video, Socket.IO is still considered the best way to implement WebSockets in 2023, as plain WebSockets may eventually require developers to build in many of the features that Socket.IO already provides out of the box.

  • What is the speaker's approach regarding Socket.IO and WebSockets?

    -The speaker's approach, even in 2023, is to focus primarily on Socket.IO as the best way to implement WebSockets, as it provides a more robust and feature-rich solution compared to implementing plain WebSockets directly.

  • What is the structure of the course mentioned in the video?

    -The course will start with some network talk, followed by a WebSocket implementation, a comparison between WebSockets and Socket.IO, and then dive into making projects using Socket.IO as the primary focus.

  • What is the purpose of the broadcasting feature in Socket.IO?

    -The broadcasting feature in Socket.IO allows you to send a message to all clients (WebSockets) connected to the server, or to a subset of clients, without having to write loops or additional code to handle this functionality.

  • What is the advantage of the multiplexing feature in Socket.IO?

    -The multiplexing feature in Socket.IO allows you to have a single connection for multiple purposes, similar to how a phone line can handle multiple calls simultaneously, without the need to establish separate WebSocket connections for each purpose.

Outlines

00:00

🌞 Introduction to Socket.IO and WebSocket in the Course Context

The opening paragraph sets the stage for a discussion about the significance of Socket.IO and WebSocket technologies in the course. It aims to clarify their roles and justify the focus on Socket.IO, emphasizing its relevance even in 2023. The instructor, acknowledging the possible eagerness of learners to dive into technical aspects, suggests that understanding the rationale behind the course's structure is beneficial. This introduction serves to prepare learners for what to expect, highlighting the decision to concentrate on Socket.IO due to its comprehensive features, despite also providing a brief overview of WebSocket for a foundational understanding.

05:02

📚 Exploring the Evolution and Comparison of WebSocket and Socket.IO

This segment delves into the historical context and technical nuances of WebSocket and Socket.IO, illustrating their evolution and current relevance. The instructor discusses how WebSocket, a native JavaScript API, offers basic real-time communication capabilities but lacks features like automatic reconnection and data packet management that Socket.IO, a library built on top of WebSocket, provides. The narrative explores the initial necessity of Socket.IO as a stabilizing layer over the inconsistent WebSocket implementations across browsers and its continued utility in addressing specific challenges not solved by WebSocket alone, such as compatibility issues, automatic reconnection, and enhanced communication features. The comparison subtly underscores Socket.IO's added value over plain WebSocket, especially in scenarios requiring robust, real-time, bi-directional communication.

10:04

🔍 Why Socket.IO Remains Relevant in 2023

The concluding paragraph reaffirms the enduring relevance of Socket.IO for implementing real-time web applications, even as WebSocket technology matures. It articulates the premise that while WebSocket now supports features once exclusive to Socket.IO, like binary data transmission, the comprehensive suite of functionalities that Socket.IO offers—ranging from fallback mechanisms for incompatible environments to features enhancing reliability and flexibility of communication—solidifies its superiority. The instructor posits that developers opting for raw WebSocket might eventually need to recreate Socket.IO's features, effectively reiterating the library's continued relevance and advocating for its use as a more pragmatic approach to real-time web communication in 2023.

Mindmap

Keywords

💡Socket.IO

Socket.IO is a JavaScript library that enables low-latency, bi-directional, and event-based communication between a client (typically a web browser) and a server. It was built on top of the WebSocket protocol and provides additional features like automatic reconnection, packet buffering, broadcasting, and multiplexing. In the video, Socket.IO is presented as the primary focus, with the narrator arguing that even in 2023, it remains the best way to implement WebSockets due to its enhanced capabilities.

💡WebSockets

WebSockets is a native JavaScript technology that allows for two-way, interactive communication sessions between a client and a server. Unlike traditional HTTP requests, which are terminated after the server's response, WebSockets establish a persistent connection, enabling real-time, event-driven communication without the need for constant polling or establishing new connections. The video compares WebSockets to Socket.IO, highlighting that while WebSockets is a native technology, Socket.IO provides additional features that may still be necessary in 2023.

💡HTTP Polling

HTTP Polling is a technique used in web applications to simulate real-time communication by periodically sending HTTP requests from the client to the server to check for updates or new data. It is a fallback mechanism used by Socket.IO when WebSockets are not supported or cannot be established, ensuring that the application can still function, albeit with higher latency and more overhead. The video mentions HTTP polling as one of the features provided by Socket.IO over plain WebSockets.

💡Automatic Reconnection

Automatic reconnection is a feature provided by Socket.IO that allows it to detect if a WebSocket connection has been lost and automatically attempt to re-establish the connection. This is beneficial as WebSockets themselves do not have a built-in mechanism for detecting disconnections, which can lead to clients displaying stale data. The video highlights automatic reconnection as one of the advantages of using Socket.IO over plain WebSockets.

💡Packet Buffering

Packet buffering is a feature of Socket.IO that ensures data packets are not lost during the reconnection process. When a connection is temporarily lost, Socket.IO will buffer any packets that need to be sent until the connection is re-established, at which point it will send those buffered packets. This prevents data loss and maintains the integrity of the communication. The video mentions packet buffering as another advantage of using Socket.IO over plain WebSockets.

💡Acknowledgments

Acknowledgments in Socket.IO allow for guaranteed delivery of messages between the client and server. When a message is sent, the sender can request an acknowledgment, which triggers a callback function on the receiving end to confirm that the message was received. This feature enables reliable communication and provides the ability to take action upon message delivery. The video presents acknowledgments as a valuable feature of Socket.IO that is not natively available in WebSockets.

💡Broadcasting

Broadcasting is a feature of Socket.IO that allows a message to be sent simultaneously to multiple connected clients or a subset of clients. This can be useful in scenarios like real-time updates, where the same information needs to be pushed to multiple recipients at once. The video highlights broadcasting as a capability that Socket.IO provides out-of-the-box, whereas implementing it with plain WebSockets would require additional code and complexity.

💡Multiplexing

Multiplexing in Socket.IO enables a single connection to be used for multiple purposes or channels of communication. This is analogous to a phone line carrying multiple phone calls simultaneously, rather than requiring a separate connection for each purpose. The video presents multiplexing as a feature that simplifies the management of multiple communication channels, whereas with plain WebSockets, separate connections would need to be established for different purposes.

💡Real-time Communication

Real-time communication refers to the ability to exchange data or messages instantly, without significant delays or the need for periodic polling. Both Socket.IO and WebSockets are designed to enable real-time communication between clients and servers, allowing for responsive, interactive applications. The video emphasizes the importance of real-time communication and positions Socket.IO and WebSockets as technologies that facilitate this capability.

💡Event-based Communication

Event-based communication is a paradigm where data is exchanged through the emission and handling of events. Both Socket.IO and WebSockets follow this approach, allowing clients and servers to send and receive messages based on specific events occurring. The video highlights event-based communication as a key characteristic of these technologies, enabling dynamic, reactive communication patterns in web applications.

Highlights

Socket IO is a library that enables low latency, bi-directional and event-based communication between a client and a server.

With socket IO and websockets, a connection is established between the client and server, allowing real-time communication without the need to establish a new connection for each message.

The websocket API is a native technology in JavaScript, while Socket IO is a library built on top of the WebSocket protocol.

Socket IO was created to bring order to the chaos when WebSockets had poor browser support and compatibility issues in the early days.

Socket IO uses WebSockets as the primary transport whenever possible but provides additional features like HTTP long polling fallback, automatic reconnection, packet buffering, acknowledgments, broadcasting, and multiplexing.

Although WebSockets now have better support, using plain WebSockets eventually leads to needing to build the additional features that Socket IO already provides.

The speaker's approach, even in 2023, is that Socket IO is still the best way to implement WebSockets due to its robust features and active development community.

The course will focus primarily on Socket IO after initially covering some network fundamentals and a WebSocket implementation for comparison.

HTTP is a request-response model where the connection is terminated after each response, like a text message conversation.

WebSockets were available in Chrome 4 (2009), but Socket IO was created in 2010 due to WebSockets' poor support and compatibility issues at the time.

Socket IO's HTTP long polling fallback feature allows it to work even when WebSockets are not supported or misconfigured.

Socket IO's automatic reconnection feature includes a heartbeat mechanism to detect disconnections and automatically reconnect with exponential back-off delay for packet buffering.

Socket IO's acknowledgments feature allows for guaranteed delivery and running code when events happen.

Socket IO's broadcasting feature enables sending messages to all connected clients or a subset of clients, which would require additional coding with plain WebSockets.

Socket IO's multiplexing feature allows for a single connection to be used for multiple purposes, unlike with plain WebSockets where separate connections would be needed.

Transcripts

play00:00

good morning developers if it's not

play00:02

morning where you are it is morning

play00:03

somewhere and it is definitely a good

play00:05

morning to learn some socket IO

play00:08

the course has the word socket IO and

play00:10

websocket in the title so the question

play00:13

is where do they both belong with regard

play00:16

to the course so the purpose of this

play00:18

video is to try and answer that question

play00:20

early rather than later there is a video

play00:23

later on in the course that will go into

play00:24

more detail but I want to want to put it

play00:27

up front here so you know what to expect

play00:29

and make my case for why the bulk of the

play00:32

course will focus on socket IO even in

play00:35

2023 we won't cover any technical or

play00:38

implementation details in this video so

play00:40

if you're itching to get started and hey

play00:43

Rob I don't care about any of this just

play00:44

teach me and I'll learn then you can

play00:46

skip this video of course I put it in

play00:48

the course for a reason so I think it's

play00:50

worth having a listen to

play00:52

so assuming you decided to stick around

play00:54

if the course has version 4 in the title

play00:57

then the main value here is going to be

play00:59

again my case for why I'm going to focus

play01:02

on socket IO and less on websockets if

play01:07

there's version two you'll get that plus

play01:10

you'll also just get an update on where

play01:12

both Technologies are at in 2023 as

play01:15

opposed to 2018 when the course was

play01:18

originally released I'm on the socket IO

play01:20

homepage here and you can see up top

play01:21

we've got uh four X's out and again we

play01:24

we started on 2X

play01:26

if I click on documentation it takes us

play01:29

to this introduction page and at the top

play01:31

here if you don't have any idea what

play01:33

either technology is we'll talk about it

play01:35

here just real quick socket IO is a

play01:37

library that enables low latency

play01:39

bi-directional and event-based

play01:42

communication between a client and a

play01:43

server so they got this cool drawing

play01:45

here HTTP which you're very used to

play01:49

right the browser or maybe using axios

play01:51

or the fetch API something like that

play01:52

will send a request to the server the

play01:56

server handles the request and sends a

play01:59

response back and then the connection is

play02:01

terminated that is the end of the that

play02:03

HTTP request if you want more

play02:05

information you need to make a new one

play02:07

which is no big deal right send a new

play02:08

request get a new response things are

play02:11

done kind of like a text message where

play02:13

you might be texting a vote or or some

play02:15

message to a system where you send it

play02:17

over the server sends it back and then

play02:20

your connection or your Communications

play02:21

over unless you send something new

play02:23

that's the end of the conversation

play02:25

with socket i o and then websockets they

play02:28

are connected all the time so we

play02:31

establish a connection here on the

play02:32

client with the server and it's more

play02:34

like a telephone call where if either

play02:37

side has something to say they just say

play02:38

there's no reason to to connect because

play02:40

you're already connected there's no

play02:42

reason for the client to have to say

play02:43

something first the server can say

play02:45

something first you get the idea okay

play02:47

it's real-time communication if for any

play02:49

reason you need real-time updates going

play02:52

either way without having to establish a

play02:54

connection that's what socket IO and web

play02:56

sockets are for so low latency

play02:59

bi-directional and event based I'm going

play03:01

to hop over here to the websocket API

play03:02

page I've got the mdn web docs up and

play03:05

the websocket API is an advanced

play03:08

technology we'll hit this later but you

play03:10

should take a nice deep breath and

play03:12

congratulate yourself you're using an

play03:14

advanced technology

play03:16

good stuff that makes it possible to

play03:18

open a two-way

play03:21

bi-directional

play03:23

interactive communication session

play03:25

between the user's browser and the

play03:27

server with this API you can send

play03:29

messages to a server and receive

play03:31

event-driven responses

play03:34

event based

play03:37

without having to pull the server for a

play03:40

reply this last one's a little bit weak

play03:42

but pull the server for reply I'm going

play03:44

to compare to low latency okay so web

play03:48

sockets and socket IO are claiming to do

play03:50

pretty much the same thing

play03:52

of these two the websocket API is the

play03:55

native technology to JavaScript so I've

play03:58

got the console open here we can do

play04:00

console.log web socket hit enter I have

play04:04

an npm installed anything no script tags

play04:06

anything this is native to the browser

play04:09

right this is built into JavaScript if

play04:11

you get JavaScript you've got websockets

play04:13

I can't do the same thing with socket IO

play04:16

that does not exist here okay we'll try

play04:19

any rendition of it that you want

play04:21

sockets are not part of JavaScript okay

play04:24

so the reason this is important is

play04:28

because for those of you like me I

play04:30

prefer to use the thing that's closer to

play04:32

the metal right the native thing if I

play04:34

can for instance I prefer to use

play04:36

JavaScript natively rather than using

play04:38

jQuery if possible because jQuery is a

play04:41

bigger library and it just takes up more

play04:43

time more resources Etc okay

play04:46

is it worth using the new thing or the

play04:49

other thing as an add-on to the native

play04:51

technology in this case that is Socket

play04:53

IO

play04:55

well for jQuery it definitely was in

play04:57

2006 because the web was a total

play04:59

disaster you had no idea what JavaScript

play05:02

was going to do in a given browser but

play05:04

jQuery standardized that

play05:06

is jQuery still necessary in 2023 well I

play05:10

hardly ever use it anymore because

play05:11

JavaScript has come so far

play05:13

it's still it's still on like 95 percent

play05:16

of websites I think that use JavaScript

play05:18

but I don't really use anymore because I

play05:20

don't need it anymore is that the

play05:22

relationship that we have between socket

play05:24

IO and websockets as web sockets come

play05:25

far enough to where we don't need socket

play05:27

i o anymore and I'll show you quick on

play05:29

the websocket page if we scroll down to

play05:31

the bottom here under browser

play05:32

compatibility

play05:33

websockets were available in Chrome 4

play05:36

Chrome 4. that goes back to 2009 I

play05:39

believe if I check Google Chrome as of

play05:42

the making of this video they're on 108

play05:44

so we've hit Triple digits so this is a

play05:47

long time ago socket IO came out the

play05:51

next year in 2010 because web sockets

play05:54

were like JavaScript in the mid-2000s

play05:56

kind of a disaster it seemed like it

play05:58

never worked like I was so excited to

play06:00

try it but it almost never never seemed

play06:02

to work soccer IO brought some order to

play06:05

that chaos just like jQuery well has

play06:08

socket IO been replaced in 2023 by

play06:11

websockets well we're going a real quick

play06:14

look at at the socket i o docs here

play06:16

first things first up at the top here

play06:18

socket eye was built on top of the

play06:20

websocket protocol okay it does some

play06:23

additional stuff and we scroll down a

play06:26

little bit here to what sakanio is not

play06:28

it says right here although socket i o

play06:30

indeed uses websocket for transport

play06:32

we'll talk about what transport is in

play06:34

the next section whenever possible okay

play06:37

so it will use websockets if it can

play06:40

but sometimes it can so scroll down a

play06:42

little bit further to features

play06:44

here are the features provided by socket

play06:46

IO over plain web sockets okay HTTP

play06:49

polling long polling fallback we'll talk

play06:51

about that later you don't don't need to

play06:52

worry about what it is this was the main

play06:54

reason that I wanted it and everybody

play06:56

else wanted it back in 2010. it was very

play07:00

common for websockets to not be

play07:02

supported and right it says here the

play07:04

browser support was in its infancy

play07:07

we're past that right 97 of browsers now

play07:09

support websockets

play07:11

but this is still a good feature because

play07:13

sometimes you get proxies that are

play07:15

misconfigured or other weird things

play07:17

where things just don't work and you

play07:19

have no idea why and this will work when

play07:23

websockets doesn't okay next thing

play07:25

automatic reconnection this is something

play07:28

a websocket doesn't do websockets

play07:30

sometimes will disconnect and the server

play07:32

and the client don't know that it's

play07:34

disconnected and so the user's looking

play07:36

at stale data okay if you need that to

play07:38

never happen socket IO has a heartbeat

play07:41

mechanism which will check to see if

play07:44

we're connected or not and if we're

play07:45

disconnected it will automatically

play07:47

reconnect okay and it has this is just

play07:50

extra awesome exponential back off delay

play07:53

meaning it will slowly send the stuff

play07:55

back in order once the connection is

play07:57

re-established so this is an add-on you

play07:59

get with socket IO that you don't get

play08:01

with websockets go down a little farther

play08:03

packet buffering this is good this has

play08:05

to do with how the packets are sent up

play08:07

after you're reconnected you can read

play08:10

about it we're not going to spend time

play08:11

on know

play08:12

acknowledgments Sakurai will give you

play08:15

the ability to guarantee that the other

play08:18

side got it or you can take action so

play08:21

the sender let's say it's the browser

play08:22

says Hey server hello world and then the

play08:25

server will run this hey I got it that

play08:28

gives you the ability to run some code

play08:29

when an event happens right via

play08:31

acknowledgments

play08:32

if we scroll down a little bit further

play08:34

broadcasting this is something that I

play08:36

use quite a bit

play08:37

it allows you to send a message to every

play08:40

websocket connected to the server or a

play08:42

socket connected to the server you can

play08:44

do this in websockets but right it's a

play08:46

little bit messy writing Loops or

play08:47

however you might try and solve it

play08:49

you can also do it to a subset of

play08:51

clients which socket IO will keep track

play08:53

of for you and then lastly here in the

play08:56

list is multiplexing this allows you to

play08:59

have a single connection

play09:01

for a bunch of purposes so you can think

play09:03

about this like a phone line a phone

play09:06

line might have a thousand phone calls

play09:07

on it at any given moment you don't need

play09:10

a thousand phone lines one for each one

play09:13

for each call you do have to do that

play09:15

with websockets you'd have to connect if

play09:17

you wanted say like an admin section

play09:19

you'd need a connection for that and

play09:21

then you could communicate on that you'd

play09:23

have to initiate a second websocket

play09:26

connection for another room which is

play09:29

maybe your main room

play09:30

we scroll down a little bit further is

play09:32

Sakurai still needed today well that's a

play09:34

fair question since websockets are

play09:36

supported in most browsers

play09:38

but and I will wholeheartedly stand

play09:40

behind this if you if you use plain

play09:42

websockets for your application

play09:44

eventually you're probably going to need

play09:47

to make all of this stuff

play09:49

which means you're making socket IO and

play09:51

that's silly because there are some

play09:53

awesome developers all over the world

play09:55

working on socket IO so that you don't

play09:56

have to do this stuff okay so that's the

play09:59

short answer as of for instance like why

play10:01

this question is even relevant it used

play10:03

to be that websockets did not support

play10:05

passing binary data it does now that was

play10:08

a socket i o feature that has gone away

play10:10

now maybe these will be replaced someday

play10:13

as the websocket API gets stronger and

play10:15

stronger but at the moment you can

play10:18

Implement native websockets but you're

play10:21

probably going to get to the point where

play10:22

you have to build these in

play10:23

and you're going to end up making a

play10:26

lousy version of socket IO my Approach

play10:28

even in 2023 is that Sakurai oh is still

play10:32

the best way to implement websockets

play10:34

okay so that's where I stand that's what

play10:37

you can expect the bulk of the course is

play10:39

going to focus on soccer IO after we get

play10:42

through a little bit of network talk we

play10:43

will go through websocket implementation

play10:46

we'll compare it to Sakurai and then

play10:48

we'll get into uh into making the

play10:49

projects thanks for watching

Rate This

5.0 / 5 (0 votes)

Related Tags
Web DevelopmentReal-time CommunicationWebSocketsSocket.IONative vs. LibraryDeveloper EducationTechnical AnalysisBrowser CompatibilityModern WebDeveloper Tools