Chapter 6 - Create a React Context to connect to the backend with Socket.IO¶
In this chapter, you'll create a React Context to connect to the backend with Socket.IO.
Info
All the following steps will be executed in the frontend
directory.
In a new terminal, you can switch to the frontend directory with the following command.
In a terminal, execute the following command(s). | |
---|---|
Steps¶
Install Socket.IO client for the frontend¶
Install Socket.IO client for the frontend with the following command.
In a terminal, execute the following command(s). | |
---|---|
Create the WebSocket Provider¶
As mentioned in a previous chapter, a provider allows to give access to some shared properties to the children of the provider. In this case, you'll create a WebSocket provider to connect to the Socket.IO gateway.
- Import Socket.IO.
- The
useMemo
function allows to keep the same reference on the socket during the entire lifecycle of the component. In other words, it allows to only have one WebSocket connected to the backend at a time. - The
useEffect
function allows to run some code only once the React component is fully initialized. - If the frontend hasn't connect to the backend yet, it connects to it now.
- Once the React component is destroyed, this function is called to close the connection to the backend.
- The socket is a dependency of the
useEffect
function. If the reference to this variable changes, theuseEffect
function will be called again.
Update the main page¶
Update the main page to use the WebSocket provider.
Info
Since the role of the WebSocketProvider
in this chapter is only to connect with the backend server, you may ask yourself why does it needs to be the child of LineProvider
and the parent of Sketch
.
The answer is "You don't choose your family".
Seriously, know that it works if the components were sorted that way:
WebSocketProvider
will needs to lose the {children}
props, but it will connect with the backend server when accessing the http://localhost:3000.
Also, why the WebSocket needs to be in a component ? It could be in a TypeScript class, the constructor
initialize the connection and methods provide a way to emit and receive message with the WebSocket connection. Yeah you're right, but experience shows that we can do better, and this tutorial shows you that way.
Check the results¶
Start the frontend and the backend services. Access the frontend on http://localhost:3000. You should see the exact same result as earlier.
Check the logs of your backend application. You should see the following message.
Reload your window. You should see the following messages.
If you close your window, you should see the following message.
To stop your applications, press Ctrl+C in your terminal.
Summary¶
Congrats! You have successfully connected your frontend to your backend! This application lacks some communication about point
and the next chapter covers that! After that, the drawing application will be working.