This project implements real-time messaging using a modular approach that supports multiple providers:
- Socket.IO (default implementation)
- Pusher (alternative implementation)
cd server
npm run devThe server will start on port 3010.
cd client/chat-whatsapp-clone
npm run devThe Next.js client will start on port 3000.
Open your browser and navigate to:
http://localhost:3000/realtime-demo
- Open two browser windows with the demo page side by side
- In one window, click the "Simulate Server Message" button
- You should see a message appear in both windows
- Try typing and sending messages in each window to see them appear in real-time
realtime/types.ts- Defines the provider interfacerealtime/providers/SocketIOProvider.ts- Socket.IO implementationrealtime/providers/PusherProvider.ts- Pusher implementationrealtime/index.ts- Factory functions for creating providers
lib/realtime/types.ts- Client-side provider interfacelib/realtime/providers/SocketIOClient.ts- Socket.IO client implementationlib/realtime/providers/PusherClient.ts- Pusher client implementationlib/realtime/index.ts- Factory functions for creating client providershooks/useRealtime.ts- React hook for using realtime in components
shared/realtime/index.ts- Shared event types used by both client and server
To switch between providers on the server, update the initialization in server/src/index.ts:
// For Socket.IO
initializeRealtimeProvider('socketio', server);
// For Pusher
initializeRealtimeProvider('pusher', {
appId: process.env.PUSHER_APP_ID!,
key: process.env.PUSHER_KEY!,
secret: process.env.PUSHER_SECRET!,
cluster: process.env.PUSHER_CLUSTER!
});To switch between providers on the client, update the initialization in components/RealtimeDemo.tsx:
// For Socket.IO
initializeRealtimeProvider('socketio', {
serverUrl: 'http://localhost:3010'
});
// For Pusher
initializeRealtimeProvider('pusher', {
key: process.env.NEXT_PUBLIC_PUSHER_KEY!,
cluster: process.env.NEXT_PUBLIC_PUSHER_CLUSTER!
});To add a new provider (e.g., Firebase, AWS IoT, etc.):
- Create a new server-side provider in
server/src/realtime/providers/ - Create a new client-side provider in
client/chat-whatsapp-clone/src/lib/realtime/providers/ - Update the factory functions in both
index.tsfiles - Add the necessary configuration options
Each provider must implement the RealtimeProvider interface on the server and RealtimeClientProvider interface on the client.