January 15, 2024
Mastering Realtime Communication
Hey there! π Ready to dive into the exciting world of real-time communication with WebSocket? π Join me in this series where we'll start from the basics, craft awesome chat/video apps, and master scaling with Redis and Kafka. Get set for a journey filled with interactive learning! ππ¬β¨ #RealTimeWeb #WebSocketAdventures
Learning about websockets can be a bit tricky at first, but once you get the hang of it, it becomes quite easy. I would definitely recommend taking the time to learn about websockets, as they are an essential part of modern web development and can greatly enhance the real-time capabilities of your applications. Plus, mastering websockets opens up a whole new world of possibilities for interactive and dynamic web experiences. So, don't be discouraged by the initial challenges β the rewards of learning websockets are well worth the effort!
This blog serves as an introduction to websockets, explaining what they are and why you should consider building your application using websockets. It also covers the difference between http and websockets, as well as the benefits of using websockets.
You can only learn by building so in this blog we will also learn how to build a basic message application using websockets with Socket.io, Node.js, and React.
What is websocket
WebSockets are a communication protocol that provides full-duplex communication channels over a single, long-lived connection. Unlike traditional HTTP connections, which are stateless and involve the client making a request and the server responding, WebSockets allow for bidirectional communication between the client and server. This is built on top of TCP/IP, which means it is reliable and ensures that every packet is received.
HTTP is like sending letters where you ask for something, and then you wait for a reply. WebSocket is like having a phone call where you can talk back and forth instantly. Chat applications, online gaming, financial platforms, and live streaming is build using websockets.
let's look at some benefits of websocket
-
Full-duplex Communication can be imagine like using walkie-talkies where you and your friend can talk and listen at the same time. Full-duplex is like having a chat where both can speak without waiting, and that's how WebSockets let computers talk back and forth instantly.
-
Persistent Connection means once you connect your device to the internet (or a server), the link stays open as long as needed. It's like staying on a call without hanging up, making communication faster and more efficient.
-
Low Latency means there's minimal delay when sending and receiving information. It's like a really fast text conversation,
WebSockets use a ws:// or wss:// URI scheme, where ws stands for WebSocket and wss is WebSocket Secure (encrypted using TLS/SSL).
Playing with websockets
You might be asking where can you start using WebSocket and how can you use this well, Modern web browsers provide a WebSocket API that allows developers to work with WebSockets using JavaScript. This API includes methods for opening a WebSocket connection, sending and receiving messages, and handling events.
wss://ws.postman-echo.com/raw*. (postman echo websocket server)
Open a web browser (Chrome, Edge, Safari, Brave) and type ctrl + shift + i to open the developer tools. Then, go to the console tab and write the following command.
As you can see, we are creating a variable called "socket" which has a URL (echo wss). We are also defining a function which will respond to the data received on the socket. We are sending a message using the "socket.send()" method.
Now, move to the network tab and find the raw name response. You can see that this raw response is of type websocket.
Clicking on raw will allow you to see additional information about the request, such as the status code and headers.
Click on the message tab to view all the WebSocket messages that have been sent and received. The UpArrow denotes the message sent by us, while the DownArrow denotes the message received. Since we are using an echo server, we are getting the same response (refer to the previous image).
Go ahead and try this out on your own and experiment with it. You can also learn more about the WebSocket API on the MND documentation.
Building a simple chat application
Learning without building is not effective, which is why we are going to create a simple chat application where users can send and receive messages in real time.
// TODO: src code // add link of github
This is a basic app without any proper UI. We will build the proper UI in future blogs. Let's start coding.
Make sure you have node installed
I am using Node.js version v20.10.0. Any version above v18.17 should work.
Create the project folder containing two sub-folders named client and server.
Server
Next, navigate into the server folder and create a package.json file.
Install Socket.io Server API,We would be using TypeScript for a better developer experience (DX) and take advantage of tsc-watch for constant reloading.
set scripts
Create a basic server using the built-in http module and start the server.
Now, in the src folder, create a folder named "services" and inside it, create a file named "socket.ts" under the "services" folder.
initListeners() method: This method initializes event listeners for the WebSocket server. In this case, it sets up a listener for the "connect" event, which fires when a client connects to the server. It logs a message indicating the user id (socket id) of the connected client.
get io() method : This getter method allows external components to access the _io instance from outside the class.
Add a event listener for message event
start the server
server work is done hereπ₯³π₯³
Client
Navigate to the client folder using your terminal and then create a new Next.js project with Tailwind CSS and TypeScript.
learn more about
- Next.js
- typescript
- tailwindcss
Install socket.io-client, socket.io a library that provides an abstraction layer on top of WebSockets, simplifying the process of creating real-time applications.
Create a folder called context and add a file SocketProvider.tsx. This file serves as a wrapper around the app, allowing us to use the socket from anywhere within the app.
working with node react and socket.io
The SocketContext defines a context named SocketContext that will hold the socket instance, messages, and a function to send messages.
The SocketProvider Component is a React functional component that wraps the entire application. It sets up the WebSocket connection, manages messages, and provides the socket context to its children.
The useEffect hook runs when the component mounts. It creates a new socket instance, sets up event listeners, and cleans up the socket and listeners when the component unmounts.
The useSocket Hook is a custom hook that uses the useContext hook to access the socket context. It throws an error if the context is not available.
Wrap the socket provider around the app to enable access to the socket from any part of the application.
Creating a Chat Application Layout and Design Using Tailwind CSS
To create the layout and design for a chat application, we will be using Tailwind CSS. In order to mark the file as a client component, we will utilize socket works as the client component only.
The handleSubmit function is called when the user submits a message. It checks if the message is not empty, clears the input field, and can potentially handle sending the message .
We can import the socket provider that we created earlier to send and display messages. useSocket() is a custom hook that enables us to interact with the state and function create.
Start the development server and open the browser to view the application at localhost
Open the application in two different tabs in incognito mode to ensure that it is working properly.
ππ Hooray! You have successfully created a chat application. This was just an introduction to websockets. In the next blog, we will explore the limitations of websockets for scaling and how to scale the websocket server using redis and kafka.