Build a Real-time Chat Application Using Socket.io and NextJS
How sockets work, what you can do with them, and how to implement the functionality with NextJS

There is always something more that you can add to your application. Maybe you’ve created a beautiful dashboard but you don’t want your user to refresh the site every time there is something new.
Maybe you want to send a notification when a new message is present. Maybe you always wanted to create a multiplayer real-time game like agar.io.
Possibilities are endless. But for different types of endeavors, you need a different set of tools. In this article I’ll walk you through the necessary steps which you have to take in order to build a real-time chat application like that:

If you’re more comfortable with working with the code, here is the Github project.
What are sockets?
There’s not really much value for knowing all of the theory underneath a socket unless you’re a Linux programmer, so let’s not dive into a rabbit hole. I’m here to give you everything you need so you can start creating cool applications. Sooooo.. what is a socket?
A socket is an endpoint of communication between 2 devices. That is a definition. In my opinion, the easiest way to understand what a socket really gives us is to put it in comparison to a non-socket communication.
Client-Server (one-way) communication

In the modern web majority of communication on the internet is of a client-server type - most APIs, and websites work in that fashion. What client-server communication really means is that a client has to ask a server in order to receive an answer. There is no way for a server to initialize that communication therefore server can’t really notify a user about any events.
Socket (dual-way) Communication

In socket communication, once it’s established both sides are free to send each other a message. There is no need to initialize communication after it was established. Now the server can notify a user about e.g. new message, and the user can react to it. Communication in that scenario is based on events.
Socket.io architecture
In order to establish dual-side communication, we need a server and a client. Since we’re using NextJS our server-side will be placed in the NextJS API folder, while the client-side will be coded into each page that will need it. So in total, we will have one server (it’s possible to have more than one server if the traffic is heavy) and one connection per client. Let’s start with the server-side.
Socket Server Code
Create a socket.tsx file in the API folder. Since the NextJS API folder acts as a server we’ll need to set one up here. We check if the socket connection is already set up by examining the res socket property. Basically, we want to create one server instance for the whole app. As you can see first we check if the server.io property is present, if so that means that we already have a server. If not we create a new one, and afterward, we assign emit handlers which I will show you in the next step.
It’s worth noting that although we can create events with almost any name there are a few special ones. The “connection” being one of them. Other ones are e.g.: “connect-error”, “disconnect”, etc. You can check the full list in the docs.
Setting different event handlers on connection is the right place to do it, because we have to make sure that the socket exists, and the callback function gets called only after the socket was created.





