Check out a free preview of the full Complete Intro to Real-Time course

The "Socket.IO Overview" Lesson is part of the full, Complete Intro to Real-Time course featured in this preview video. Here's what you'd learn in this lesson:

Brian introduces Socket.IO and compares it to the ws socket library. Socket.IO is a framework designed around WebSockets because it manages connection pools, can send large files like photos and videos, and adds middleware for auth or rate limiting.

Preview
Close

Transcript from the "Socket.IO Overview" Lesson

[00:00:00]
>> We are going to get into here in real time exercises WebSockets exercise socket.io that is your next set of exercises that you need to be on and I'm going to go to Pitt deli/intro, real time. We're down here on intro to socket.io. So we went through and we did all those that socket stuff by hand, it is not fun in the sense of like this was fun to write, but there's absolutely nothing that I would want to ever maintain in production, right?

[00:00:44]
There's just a lot that can go wrong there, right, there's a lot of just surface areas for bugs that you could have. It'd be way better if we could get someone, some open source community to maintain some really nice library that handles all this for us, then we could all contribute just to the same thing.

[00:01:01]
For WebSockets, that means probably one of two libraries that you would use one would be socket.io, which is the one that we're about to use. And one would be WS, which is WebSockets as you might have guessed, let's just chat a second about Ws. It's an awesome library and I'll be honest, most of the time I choose Ws over socket IO personally.

[00:01:26]
But it's like the difference between having like a really nice helper library for WebSockets and a framework on top of WebSockets, right? So Ws is just like hey, I need a WebSocket and I'm Fitting this into another piece of a bigger system that I have,it's mostly what I do, right?

[00:01:45]
Then Ws is perfect for that, they basically take that web socket interface, that we have on the client that we wrote together, right? And they kind of make that same sort of like minimal interface, for the server, and that's really all that ws does. It's an awesome library one that you should definitely go give a shot after we do all this together, it's great.

[00:02:06]
I have nothing but good things to say about them and their maintainers. The other one that is probably more popular is called socket.io socket.io is like a full on framework for WebSockets so the it it just adds a lot more functionality. Like there's things like you can add like rooms, for example, right?

[00:02:29]
Like if you had, you know, 10 different chat rooms, socket.io has the ability to have the concept of rooms that I'm sticking this group of users on this room and this group of users in this room. They can subscribe to different channels and different messages and things like that, like pub sub, if you've ever heard that concept before, is like built into socket.io.

[00:02:50]
pub sub is this idea of Publish and subscribe, so it's kind of basically what we were doing between the client and the server. They basically have a bunch of clients that subscribe to like a topics of ideas, right, so it does a bunch of really interesting things like that.

[00:03:06]
Like if I was gonna today go make like a Google Docs like collaborative editing environment probably use something like socket.io because there's so much functionality that they built into that. That would be very useful for some large scale real skit like real time application. Whereas like, if I'm just doing like a WebSocket to share my if I'm online or not, I wouldn't use socket io for such a small task, I would use Ws.

[00:03:40]
Actually Ws is so small that they don't even have a client library you actually use that new WebSocket thing that we were using on the client, right? So that's how minimalistic it is, so, if you ask me which one I prefer the answer is just it depends. Do I have a big project or a small project, big projects socket.io small projects Ws are maybe features a better way of putting that.

[00:04:05]
What else socket.io does middleware like you know like with express.js, you can actually add middleware's in there to like do things in the middle between of when you receive a request when you reply to it socket.io has that built in. It has great support for sending binary like large binary objects like photos, audio video that works really well.

[00:04:27]
socket.io something cool that we actually will see with socket.io It does fall back, so let's say you connect to a user. And that user is using, I don't know, Internet Explorer eight, where it doesn't support WebSockets, but it does support long polling. It'll actually behind the scenes switch to long polling for you, so that that user can still get real time information, that it it'll just be long polling, socket.io has an audit built in.

[00:04:58]
You actually don't have to change anything about your code, socket.io has like this cool kind of establishes a connection and then makes connections for and does polling to get that information. Yeah, socket.io has the ability to like connect to MongoDB directly so you can actually do change feed directly from Mongo, from Redis.

[00:05:21]
You can get real time change feeds from Redis or Postgres or side socket.io has client ID connect with all these databases, directly. So that's just some of the difference, whereas WS is just as WebSockets, doesn't do anything else, just WebSockets.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now