Chapter 5 - Create a WebSocket gateway with NestJS and Socket.IO¶
WebSocket is a protocol that allows bi-directional communications. That means your frontend service can connect to the backend service and both can send/receive data anytime.
This protocol will be used in the drawing application to send all the points drawn by the players to the rest of connected players, in real-time.
Socket.IO is a library that uses WebSocket protocol, enabling the creation of interactive applications with Node.js and the browser.
In this chapter, you'll install and configure a Socket.IO gateway with NestJS and validate it works with the cURL command-line tool.
Info
All the following steps will be executed in the backend
directory.
In a new terminal, you can switch to the backend directory with the following command.
In a terminal, execute the following command(s). | |
---|---|
Steps¶
Install Socket.IO¶
Install Socket.IO and NestJS WebSocket support with the following command.
In a terminal, execute the following command(s). | |
---|---|
Create the backend types¶
Resembling the steps done for the frontend, create a new types
directory to store all the types needed by the backend.
In future chapters, you'll improve the management of the types with a shared project. But for now, duplicate the code in both project.
Create the WebSocket gateway¶
Create the WebSocket gateway with NestJS.
- This method will be called whenever a WebSocket connects to your backend.
- This method will be called whenever a WebSocket disconnects from your backend.
- This methods will be called whenever a WebSocket sends the event
FIRST_POINT_FROM_PLAYER
. - The
@ConnectedSocket()
decorator is able to extract the WebSocket from the request. - The
@MessageBody()
decorator is able to extract the payload sent by the WebSocket from the request. In this case, a point (point
). - The socket broadcasts to all other players connected to your backend the event
FIRST_POINT_TO_PLAYERS
with the point the player did. They will all receive the eventFIRST_POINT_TO_PLAYERS
with the point (point
). - This method will be called whenever a WebSocket sends the event
POINT_FROM_PLAYER
. - The socket broadcasts to all other players connected to your backend the event
POINT_TO_PLAYERS
with the point the player did. They will all receive the eventPOINT_TO_PLAYERS
with the point (point
).
Add the WebSocket gateway to NestJS¶
backend/src/app.module.ts | |
---|---|
Try out the WebSocket gateway¶
Start the application.
The output should be similar to this.
Try to connect to the backend using the WebSocket protocol with cURL.
The output should be similar to this.
Output of the cURL command. | |
---|---|
The command line will not be responsive for ~30 seconds. You can cancel the command by pressing Ctrl+C in your terminal.
No logs will be shown in the NestJS application but cURL can ensure it will work in the next chapter (and you'll see messages on the NestJS side :))
To stop your NestJS application, press Ctrl+C in your terminal.
Summary¶
Congrats! You now have a NestJS application that can wait for WebSocket connections. Using WebSocket will enable the bi-directional communication between the backend and the frontend in the next chapter.