- Overview
- Transcript
1.2 What Are WebSockets?
In this lesson I'll take a moment to explain WebSockets. We won't need to know the low-level details of WebSockets, but it helps to understand what's going on behind the scenes. I'll also give you an overview of the current browser support for WebSockets.
1.Introducing WebSockets and Socket.IO2 lessons, 06:06
1.1Introduction01:23
1.2What Are WebSockets?04:43
2.Scaffolding a Socket.IO Application4 lessons, 15:21
2.1Installing Node.js and App Dependencies02:59
2.2Creating an Express Server04:35
2.3Adding WebSockets to the Back-End03:00
2.4Connecting the Front-End Application to Socket.IO04:47
3.Building the Front-End Application7 lessons, 32:59
3.1Receiving Socket.IO Events03:20
3.2Displaying Messages06:22
3.3Creating Messages and Updating Collections07:50
3.4Displaying Likes02:31
3.5Providing Users With Unique Ids03:41
3.6Updating Like Counts05:03
3.7Propagating Like Counts to Connected Users04:12
4.Completing the Application2 lessons, 07:22
4.1Adding Moment.js04:13
4.2Adding Styles03:09
5.Conclusion1 lesson, 05:52
5.1Conclusion05:52
1.2 What Are WebSockets?
We must ask ourselves, what are WebSockets? Well, whenever you use any internet thing, even if it's not a WebSocket, if you just visit a browser, you're basically making a connection between two computers. Your computer must connect to the server, say Facebook Server. Even if just for a minute to exchange the data that your computer wants. WebSockets are a way of doing this kind of connection I just described, but in a persistent manner. WebSockets share data quickly. Now, with the internet, if I want to share my data with you and you're a computer and I'm a computer, I would first have to send you a request saying hello, and then you reply in computer language, hello. And I'd reply, I'd like to send you a file. And you'd might reply, what kind of file that is and then I'd reply, and maybe after four or five little chatters, I'd send the file. But then maybe you'd say, that's great. Okay, send over another. And if we don't have a protocol in place, we have to repeat the same chatter. I have to tell you, what kind of file it is, who I am, etc., etc. Which if we're dealing with thousands of files, is very slow. WebSockets to the rescue. With WebSockets, you only need to create the connection once. That's why it's called a persistent connection. Once you connect to a WebSocket, you can send data both ways quickly and efficiently. Not only is this a fast and performant way to build our websites, but it's very easy to understand and reason about when writing the code. So do take note that WebSockets are not for every website. If you're building a simple website with, say, just a list of your favorite animals, then you don't really need WebSockets. WebSockets are mostly for interactivity. So in conclusion, if you have a websites where you need to exchange lots of data between the browser and the server, let's say a chat messaging app for example, then you're gonna want a seriously look into WebSockets. So before we begin, we need to have an intelligent discussion about WebSocket support. Why? Because if you're, say, the lead developer of some major company and you decide to implement WebSockets, well, it's your responsibility to know if this technology is actually going to work. Many technologies on the internet don't work on all devices. As an obvious example, Flash, which isn't a real internet technology, doesn't work on iPhone devices or most devices that aren't computers. So in the same way you might choose to do your application Flash or an HTML5 based on your audience. You need to make the same decision about WebSockets. Here on this page we can see the can I use graph for WebSockets. Can I use a website which automatically tests various libraries against a number of different browsers? If the tests pass then the field looks green and you can use it if you're supporting that browser. So we can see that WebSocket Support has three weak points, IE8 and 9, Opera Mini, and a couple of versions of Android browser. So if you're building a project and you must support old version of Internet Explorer, then WebSockets probably aren't for you. Now keep in mind that most libraries provide a shim over WebSockets. So they'll give you an API to use, but if WebSockets aren't available, they'll fall back to a different way of synchronizing data. Remember the example earlier about the long conversation where every time I have to tell you who I am and what I want? Well, that's a thing called Ajax, and you use it all the time in the websites you visit. In fact, you probably use it a lot more than you should, because most of that could probably be done with WebSockets. However, Ajax works, so for a lot of websites it's totally fine. At the end of this series of videos, though, you are going to be totally prepared to use WebSockets or Ajax, whichever one suits you best. So, I hope you'll stick with us for this exciting course and in the next video we'll get started by implementing the scaffolding for application.







