Building a Real-Time Chat App with Next.js and Socket.io (App Router)

Search for a command to run...

No comments yet. Be the first to comment.
When it comes to building modern web applications, Next.js stands out for its simplicity and developer-friendly features. One of the core features that makes Next.js so powerful is its file-based routing system. Unlike traditional routing methods tha...
If youβre building modern web applications, chances are youβve needed a simple backend for tasks like storing form submissions, feedback requests, or lightweight data logs. Instead of spinning up a server or managing databases, what if you could just...

Managing global state in a Next.js application can be tricky, but Redux Toolkit (RTK) makes it easier by reducing boilerplate and improving performance. In this guide, weβll walk through integrating Redux Toolkit with Next.js to efficiently manage ap...

Firebase is a powerful backend-as-a-service platform that offers authentication, real-time databases, and hosting. Integrating Firebase with a Next.js application allows you to build scalable and serverless web applications with ease. Why Use Fireba...

Optimization in Next.js App

Real-time applications, such as chat apps, require instant data exchange between clients and servers. In this guide, we'll walk through integrating Socket.io with Next.js (App Router) to build a real-time chat application.
Socket.io is a powerful WebSocket library that enables bi-directional communication between the client and server. Next.js App Router (app/) does not support API routes, so we need a separate WebSocket server.
Low Latency: Instant message delivery between users.
Separate WebSocket Server: Enhances scalability and flexibility.
Works with App Router: Since API routes are not available in app/, using a dedicated WebSocket server is ideal.
Run the following command to set up a Next.js project:
npx create-next-app@latest nextjs-chat-app --typescript
cd nextjs-chat-app
You'll need socket.io for both the server and client:
npm install socket.io socket.io-client
Since API routes are not available in App Router, we must run a separate WebSocket server.
server.tsπ Create a server.ts file in the root directory:
import { createServer } from 'http';
import { Server as SocketServer } from 'socket.io';
const httpServer = createServer();
const io = new SocketServer(httpServer, {
cors: { origin: '*' },
});
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('message', (msg) => {
console.log('Received message:', msg);
io.emit('message', msg);
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
// Start WebSocket server on port 3001
httpServer.listen(3001, () => {
console.log('WebSocket server running on port 3001');
});
This server listens for WebSocket connections and handles real-time messaging.
π Create Chat.tsx in app/components/Chat.tsx:
'use client';
import { useEffect, useState } from 'react';
import { io, Socket } from 'socket.io-client';
let socket: Socket;
export default function Chat() {
const [message, setMessage] = useState('');
const [messages, setMessages] = useState<string[]>([]);
useEffect(() => {
socket = io('http://localhost:3001'); // Connect to WebSocket server
socket.on('connect', () => {
console.log('Connected:', socket.id);
});
socket.on('message', (msg) => {
setMessages((prev) => [...prev, msg]);
});
return () => {
socket.disconnect();
};
}, []);
const sendMessage = () => {
if (socket && message.trim()) {
socket.emit('message', message);
setMessage('');
}
};
return (
<div>
<h2>Real-Time Chat</h2>
<div>
{messages.map((msg, index) => (
<p key={index}>{msg}</p>
))}
</div>
<input
type="text"
value={message}
onChange={(e) => setMessage(e.target.value)}
/>
<button onClick={sendMessage}>Send</button>
</div>
);
}
This component connects to the WebSocket server, listens for messages, and allows users to send chat messages.
π Modify app/page.tsx to include the chat:
import Chat from './components/Chat';
export default function Home() {
return (
<div>
<h1>Next.js Real-Time Chat App</h1>
<Chat />
</div>
);
}
Users will exchange messages in real-time through WebSockets. Hereβs how it works:
Client connects to the WebSocket server:
Chat.tsx component initializes a connection to the WebSocket server running on http://localhost:3001.Sending messages:
When a user types a message and clicks "Send," the message is emitted using socket.emit('message', message).
The server receives this message via the socket.on('message', (msg) => { ... }) listener.
Broadcasting messages:
The server listens for incoming messages and then emits the message to all connected clients using io.emit('message', msg).
This ensures that every client (including the sender) receives the message in real time.
Receiving messages on the client:
Every connected client listens for new messages with socket.on('message', (msg) => { setMessages([...prevMessages, msg]) }).
This updates the UI instantly with the received message.
By following this mechanism, multiple users can exchange messages in real time seamlessly.
Before running your Next.js app, start the WebSocket server:
node server.ts
Run Next.js separately:
npm run dev
Visit http://localhost:3000, open multiple browser tabs, and chat in real-time!
By integrating Socket.io with Next.js (App Router), you've built a fully functional real-time chat application. This guide covered:
Setting up a separate WebSocket server for Next.js App Router.
Connecting the client to the WebSocket server using Socket.io.
Implementing real-time messaging in a React component.
β Add User Authentication: Secure messages for logged-in users. β Store Chat History: Use Firebase, PostgreSQL, or MongoDB. β Implement Group Chats & Notifications.
