Looking to build scalable and real-time applications in one of the simplest ways possible? You’ve come just to the right place!
This is one of Packt’s Video Learning Paths which is a series of individual video products put together in a logical and stepwise manner such that each video builds on the skills learned in the video before it.
In this Learning Path, we look at implementing real-time applications on websites using SocketIO, Express, and Redis.
SocketIO has been used in creating many real-time chat and blog applications because of its powerful real-time engine. Yes, Socket. IO features one of the fastest and most reliable real-time engines. This makes it almost perfect for real-time analytics, instant messaging, and two-way communication between the browser and web application. Redis opens doors by making data quick and easy to fetch. Lastly, Express helps by making full-featured web applications easy. A powerful combination of these three will give you awe-inspiring application experiences and you’ll be able to create fast, scalable, and dynamic real-time apps in no time!
Essentially, this Learning Path is a practical guide on integrating Redis and NodeJS and making a site realtime using Node.js and SocketIO.
By the end of the Learning Path, you will be a competent SocketIO developer and will be able to create fast, scalable, and dynamic real-time apps.
About the authors:
This Learning Path, contains the best works of Joshua Johanan and Tyson Cadenhead who are experienced at creating applications.
What is Redis/Redis CLI?
Redis data types.
What is the Redis commands?
Redis does not have traditional indexes.
Redis does simple messaging. We cover what that means and how to do it.
How to receive messages from Redis PUBSUB.
Using Redis PUBSUB.
How do we use what we know about Redis with Node.js?
Using PUBSUB in Node.js.
Many times, Redis has data that is stored across multiple keys. This makes getting the data difficult.
How do we use sorted sets?
Using geospatial indexes with Redis.
This is our first introduction to Socket.io.
Building a simple Socket.io application.
How do we communicate with more than one browser?
We can send to every connection, but sometimes we want every connection but ours.
One more event to tie everything together.
What are rooms and why use them?
What are namespaces and why use them?
How to use rooms in an actual application.
How to use namespaces in an application.
We are now going to start integrating Socket.io and Express together.
Integrated Socket.io and Express do not share requests. How do we get them to work together?
How to use Socket.io events and Express routes together.
Many sites today have multiple servers that run them.
Sending messages from other processes and even other languages.
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.
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.
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.
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.
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.