Prerequisites
- Node.js 16 or higher
- Redis instance (setup instructions)
- Next.js or Express.js project
Installation
Next.js Setup
1. Create API Route
Create a catch-all API route atapp/api/socket/[[...path]]/route.ts:
2. Set Environment Variable
Create.env.local:
3. Create Client Component
Createapp/components/SocketClient.tsx:
4. Use in Page
Updateapp/page.tsx:
5. Run the Application
http://localhost:3000 to test the implementation.
Express.js Setup
1. Create Server
Createserver.ts:
2. Create Client
Createpublic/index.html:
Next Steps
- Architecture - Understanding the system design
- Rooms - Broadcasting to client groups
- Redis Setup - Production configuration
- API Reference - Complete API documentation
Troubleshooting
Redis connection errors: Verify Redis is running andREDIS_URL is set correctly:
app/api/socket/[[...path]]/route.ts
Messages not received:
Event names are case-sensitive. Verify both client and server use identical event names.