This video course is a complete resource, covering topics from WebSocket security to scaling the server-side of a Socket.IO application and everything in between.
This video course will provide real-world examples of how secure bi-directional, full-duplex connections that can be created using Socket.IO for different environments. It will also explain how the connection vulnerabilities can be resolved for large numbers of users and huge amounts of data/messages.
By the end of the course, you will be a competent Socket.IO developer. With the help of the examples and real-world solutions, you will learn to create fast, scalable, and dynamic real-time apps by creating efficient messaging systems between the server side and the client side using Socket.IO.
About The Author
Tyson lives in the greater Nashville area with his wife and two sons, where he enjoys gardening, raising chickens, reading philosophy and economics books, and playing guitar.
In order to get Socket.IO running, we need to have at least one client and one server set up to talk to each other.
Express is probably the most widely used Node application framework and we can use Socket.IO based on an Express server.
The native WebSocket implementation in browsers is much less robust than what Socket.IO offers. We'll set up Socket.IO as a Web Socket.
Socket.IO gives us the ability to toggle certain parts of our logging on and off as needed. Let's dive into debugging our code.
We will create an application that emits a static object. The client side doesn't concern itself with the frequency of state changes, so the difference between emitting data once and emitting data frequently is inconsequential. If we can re-render the state of a dashboard once, we can re-render it many times.
It is sometimes useful to have the date and time from the server side instead of the client side.
As we are now able to send static data to our client with Socket.IO, we have all the tools we need to send dynamic data as well.
Socket.IO excels at creating rich real-time analytic dashboards. It also has some really great built-in functionality to re-establish a server-side connection once it has been dropped.
The goal of this video is to create a simple chat room using Socket.IO.
We are going to manage the socket life cycle in this video
Sometimes, you need to send a private message to just one other socket and not every socket that may be listening in. As the server side is in charge of managing all the connected sockets, we can specify the sockets that our events are emitted to granularly. In this video, we will use this ability to create a simple app.
When a socket sends a message, we don't necessarily want it to receive the message that it sent. We may want to display a different message to the sender than to the receivers. This can be accomplished using the socket broadcast emit syntax, which we will look at in this video.
We can observe the real power of Socket.IO by using it in a multiplayer game. In this video, we will just do that in a game of tic-tac-toe.
Many applications have multiple customers, which should never be mixed. In the same way, our Socket.IO sockets can be namespaced to minimize concerns about intermingling data and messaging. Let's look at how to create namespaces.
In addition to namespaces, we can use Socket.IO rooms to ensure that our messages are being delivered to the correct sockets.
If you're building a real-time application, it's important that a socket can not only join a room but also leave any room that it is a member of.
Socket.IO provides a dynamic list of rooms that each socket is a member of. We can retrieve this list and make sure it updates dynamically when the socket leaves a room.
Although Socket.IO doesn't have any inbuilt way to consider a room as private or public, we can add some logic around joining a room so that only sockets that validate against a password check are allowed to be members of the room.
In Socket.IO, every socket that makes a connection is assigned a default room to emit messages. This default room could be used for a wide variety of purposes.
Most applications need a way to authenticate users. In this video, we will create a simple form to create and authenticate users.
Now that we have been able to perform basic authentication with Socket.IO, let's take a look at a token-based approach that handles authentication more securely, such as JSON Web Tokens, or JWT.
When we write data to a database, it is important to perform validation on the server side to ensure that the data is in the type and format that we expect it to be in. In this video, we will demonstrate how we can emit data to the server and emit messages back if there is success or an error.
There are some instances where you may not want your Socket.IO events to be available to every other domain. Not to worry! We can easily whitelist only the HTTP referrers that we want so that some domains will be allowed to connect and other domains won't.
If your application uses the HTTPS protocol, you will also need to use the wss protocol for your Web Sockets. Let's see it in action.
A single-node server can typically handle several thousand simultaneous connections. However, as the audience of an application grows, it is important to make sure that the application is scalable. In this video, we'll see Nginx in action performing load balancing.
Node.js comes with a cluster package that can be used to run Node on multiple threads, as opposed to the single thread that it runs on normally. Unfortunately, there is some boilerplate needed to determine the number of CPUs available to run Node processes and fork the original node. For this, we can use a module called sticky session. Let's see it in action in this video.
Now that we are able to run multiple nodes simultaneously with Socket.IO and not lose our socket connection between events, we will also need a way to ensure that when an event is emitted on one node, it is also emitted across all of our other nodes. For a distributed system, we will need to use an adapter that lives outside of our server nodes. Redis is a perfect solution for this problem; let's see how.
In this video, we will use Memcache to propagate events across multiple server nodes.
In this video, we will use RabbitMQ, which allows you to use multiple servers and broadcast messages across them.
We can use Socket.IO to send images from a browser to a server. We can then display them in another browser without storing them on a server, a filesystem, or a database of any kind. In instances where we don't need the data to be stored, this can be really useful.
Using Socket.IO, we can send files to our server over WebSockets instead of an http POST request. Let's see how.
If you are deploying the app from your repo, uploading images to your server-side file isn't advised. A much better approach is to put the media in a separate location, such as Amazon S3.
Streaming images with Socket.IO is great. However, we can also use WebSockets in combination with WebRTC to stream audio from one user's microphone to another.
While streaming audio is great, live video is even more gratifying.
We can use our central server-side location as an API for multiple applications.
When you have a huge amount of data, it is advisable to create a paged format so that the data won't be loaded all at once.
Use the power of Socket.IO to trigger “Hot Deploys” and refresh the code in the Cordova web view without needing the resubmit their app to the app store.
Packt has been committed to developer learning since 2004. A lot has changed in software since then - but Packt has remained responsive to these changes, continuing to look forward at the trends and tools defining the way we work and live. And how to put them to work.
With an extensive library of content - more than 4000 books and video courses -Packt's mission is to help developers stay relevant in a rapidly changing world. From new web frameworks and programming languages, to cutting edge data analytics, and DevOps, Packt takes software professionals in every field to what's important to them now.
From skills that will help you to develop and future proof your career to immediate solutions to every day tech challenges, Packt is a go-to resource to make you a better, smarter developer.
Packt Udemy courses continue this tradition, bringing you comprehensive yet concise video courses straight from the experts.