How to use WebSocket in NextJS App router with Socket.IO
Posted by
kamlesh paulon
Dec 26, 2024| 9 min read
Last updated on : Dec 26, 2024
Table of contents
- Introduction
- Method 1: Using a Custom Server in Next.js
- Method 2: Using a Separate WebSocket Server (Recommended)
- Connecting Your Frontend
- Wrapping Up
- FAQs
Introduction
WebSockets are amazing when you want to add real-time features like chat, notifications, or live data updates to your web application. If you're working with Next.js App Router, you might wonder how to use WebSockets with it.
That’s where Socket.IO comes in! It’s a library that makes working with WebSockets super easy.
In this guide, we’ll cover two ways to use WebSockets with Next.js:
- Nextjs with custom server : This is a simple option, but it’s not ideal for big projects
- Separate WebSocket Server: A more scalable and flexible setup.(Recommended)
By the end of this, you’ll know how to implement both methods and when to use each one. Ready? Let’s get started!
Method 1: Using a Custom Server in Next.js
Dependencies
socket.io
for WebSocket functionalitytsx
to run the development server:tsx server.ts
Setting Up the Custom Server with Socket.IO
Create a file named server.ts
in the root of your project
We will handle the sockets
logic in a separate file: server/sockets/index.ts
Adding a TypeScript Configuration for the Server
Create a tsconfig.server.json
file in the root of your project :
Update your existing tsconfig.json
file as follows
Updating package.json
Scripts:
Add the following scripts to your package.json
:
Reference
For more details on using a custom server in Next.js, refer to the Next.js Custom Server Documentation.
Method 2: Using a Separate WebSocket Server (Recommended)
Why Use a Separate Server?
Keeping a separate server for WebSockets is a smart choice because:
- No Need for a Custom Next.js Server: This means your setup stays future-proof, and you avoid messing with Next.js optimized framework features.
- Handles More Users: You can scale the WebSocket server without affecting your Next.js app.
- Organized Code: Your Next.js app focuses on pages, and the server handles real-time updates.
Even Lee Robinson, a core member of the Vercel team, doesn’t recommend using a custom server with Next.js. So, using a separate WebSocket server is a cleaner, future-proof way to go!
Building the WebSocket Server
Create a Separate Directory : Make a new folder called websocket
and run:
Install Dependencies
Create app.js
This code initializes a WebSocket server on port 3005 and uses the same sockets/index.js
file to handle socket-related events.
Update package.json
:
Add the following changes:
Reference doc https://socket.io/docs/v4/server-initialization/
Connecting Your Frontend
To integrate WebSocket functionality into your Next.js frontend, we’ll create a reusable SocketProvider
component. This component establishes a single WebSocket connection when the app loads, making it available across the application.
Creating SocketProvider.tsx
Usage
Wrap your application with the SocketProvider
in layout.tsx
to ensure the WebSocket connection is available throughout the app.
- Using the WebSocket in a Component
Testing
We’ll add a new live-chat
page to the Next.js Role and Permission repo to demonstrate WebSocket functionality.
Here’s the complete code for the live-chat
page
In this example, there are two things happening:
- Listening for Messages: Inside the
useEffect()
hook, the client listens for anysendMessage
event. When a message is received, it updates themessages
state with the new message:
- Emitting Messages: In the
sendMessage()
method, the client emits asendMessage
event containing the message content. The server handles this by broadcasting the message to all other connected clients:
The server-side logic looks like this:
This setup creates a real-time chat functionality where messages are dynamically sent and received without needing a database.
All the code mentioned in this example is available on the https://github.com/Kamleshpaul/nextjs-role-and-permission
Wrapping Up
In this guide, we explored two ways to integrate WebSockets into your Next.js project:
- Custom Server Approach - Ideal for smaller applications but limited in scalability and compatibility with Next.js optimizations.
- Separate WebSocket Server - A scalable, flexible, and recommended approach for production-ready applications.
We also demonstrated how to integrate WebSocket functionality into your frontend using a reusable SocketProvider
and tested the implementation with a live chat feature.
Both methods have their place depending on your project’s requirements. For long-term scalability and maintainability, the separate WebSocket server is generally the better choice.
FAQs
Why use a separate WebSocket server instead of integrating it with Next.js?
A separate WebSocket server allows better scalability and keeps your Next.js app optimized for serverless deployment. It also aligns with Next.js best practices by avoiding the use of custom servers.
Can I use this WebSocket setup with a hosted Next.js app on Vercel?
No, Vercel does not support WebSocket connections in its serverless environment. You'll need to host your WebSocket server on a different platform, such as AWS, DigitalOcean, or a dedicated Node.js server.
Reference: Vercel's WebSocket limitations.
What happens if the WebSocket server is down?
If the WebSocket server is unreachable, the frontend won't be able to establish or maintain the real-time connection. You can handle such cases gracefully by notifying users or falling back to other communication methods.
Can I use libraries other than socket.io for WebSockets?
Absolutely! Libraries like ws
or native WebSocket APIs can also be used. However, socket.io
simplifies setup and offers robust features like event handling and broadcasting.
Where should I deploy the WebSocket server?
You can deploy it on any platform supporting Node.js, such as AWS
, DigitalOcean
, or even your local server
, depending on your scaling needs.
Get updates directly to your inbox.
Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.