Let's create a chat app, using SocketIO and Express for our backend with any frontend.
An introduction to RTC
Real-time communication, or RTC for short, describes live communications with
little to no latency. It's used for messaging, notifications and much more.
Today, we'll explore how to set up our own server for a basic chat app between
two different users.
Setting up the frontend
Since this article is focused around the backend, I won't go into details about
creating the frontend or how to structure it. I'll be using SvelteKit but it
won't differ a lot for integrations with other frameworks such as NextJS or
NuxtJS, or even in vanilla JS.
Creating our express app
First, initialize the package.json file.
Terminal
Next, modify/add the following inside the package.json file.
package.json
Let's install the necessary dependencies.
Terminal
This will install cors for handling Cross-Origin requests to our server,
dotenv to load environment variables inside our app, express for writing
the app itself and socket.io for enabling real-time communication.
It will also add the necessary types for our app.
Create a tsconfig.json file manually or with npx tsc --init and modify its
content with the following. Feel free to adjust anything.
tsconfig.json
Now, add ts-loader.js file in the root of the express app. This will allow
us to not encounter issues with esm and ts.
ts-loader.js
Add a .env file and fill it with the port you'll use.
.env
Then, let's define our main src/app.ts file.
app.ts
Finally, start the app and go to the server's url. You'll be greeted by Express.
Terminal
Implementing SocketIO
Now, let's add SocketIO inside our server and our frontend.
Inside Express
Adapt our express app with the following.
app.ts
We're adding an HTTP server, on which we'll listen instead of the express app
and SocketIO to listen for incoming connections and disconnections.
Inside our frontend
First, install the client for socket.io.
Terminal
Next, create a socket.ts file from which we'll import our socket.
socket.ts
Now, import the initSocket function inside the page and call it after it
mounts. Adapt it to your frontend.
+page.svelte
You should now see logs in the express app when you reload the page.
Sending messages
Finally, we'll work on sending a message from a client and transmit it to the
other connected users, kind of like a group chat (PS: if you want to send it
to a specific client, you'll need to store a map of userIds/sessionIds, and
broadcast it with the to method, however I won't go into details since it's
more of an introduction to websockets).
Inside our server
Inside the io.connection function and after the socket.on("disconnect")
call, we'll have a socket event to receive a message and broadcast it to
everyone else including ourselves. Usually, you'd hook up a database to store
the messages since they will disappear after a page reload.
app.ts
Inside our frontend
Let's update our page to send messages, receive and display them.
+page.svelte
You can test it by opening two separate tabs/browsers and sending a message.
The other one will also update !
Conclusion
We now have our own websocket server fired up and ready to build the next
social media ! I'm just kidding.. In any case, you can do a lot of things with
SocketIO. Check their
official documentation to
know more.