January 22, 2020
| Purpose | To create a real-time chat application and learn the basics of Sockets.io |
| Project Dates | January 22, 2020 |
| Technologies | NodeJs/Express, React, Styled Components, JavaScript, Socket.io |
| Repository | https://github.com/meechanism/chatapp |
I haven’t played around with Socket.io’s event-driven real-time API before. I thought it would be a perfect candidate for creating a chat application.
The idea was to have a simple web-based chat application relay messages between users.
I decided to use Create React App to quickly get the web application running and removed most of the boilerplate.
I used styled-components to create declarative, functional, and composable units of UI components.
To handle state, I used React Hooks.
Socket.io’s client was used to connect and manage the socket on the client-side.
For the server, I used ExpressJS + Socket.io to handle realtime events. The server will also handle some simple user management.
I found the Socket.io client api to be fairly straight-forward. Their documentation provides ample examples and descriptions for writing the basic needs of this chat application.
At first, I wasn’t sure how and where I would integrate the client-side, but it turns out all you have to do is add a couple useEffects to handle some events.
Add a useEffect for handling when the component renders. Within it, we want to do two important things:
join event and passing along that data. The server can then do some user management.Additionally, we only want to set up the socket and join for a user once, so for the initial useEffect, add the socket endpoint and user data as the re-render skip-effect arguments. Example:
useEffect(() => {
// ...
}, [IO_ENDPOINT, USER_DATA]);
useEffect that will emit a disconnect method so your server will handle that user appropriately.message event with the on method.message and provide the user and message as payload.The server will take care of the rest!
For setting up Sockets on the server, we need to do the following:
Set up an event on listener for the connection event.
When we connect, we are provided a socket in the callback in which we can listen to the following events and handle them appropriately:
joins a channelsendMessageThis was very much an exercise in using Sockets.io, but it was a good start to a chat application foundation. In some future iterations, I can add the following changes:
If you want to check out the project, download the chatapp over at the Github repository!