Menu

Post image 1
Post image 2
1 / 2
0

I built the missing NestJS npm package: real-time user presence tracking with Socket.IO and Redis

DEV Community·Saifuddin Tipu·25 days ago
#DxON99Dw
Reading 0:00
15s threshold

I built the missing NestJS npm package: real-time user presence tracking with Socket.IO and Redis If you've built a chat app, a support platform, or any collaborative tool with NestJS, you've probably written some version of this code: typescript // In your gateway... async handleConnection(client: Socket) { const userId = client.handshake.auth.userId; await this.redis.set(`presence:${userId}`, '1', 'EX', 30); this.server.emit('user:online', { userId }); } async handleDisconnect(client: Socket) { const userId = client.handshake.auth.userId; await this.redis.del(`presence:${userId}`); this.server.emit('user:offline', { userId }); } And then you realize: what about users with multiple tabs open? What about ungraceful disconnects that never fire the disconnect event? What about querying who's online in a specific room? What about running this across 3 horizontally-scaled pods? I've written this code — or a more complex version of it — on every real-time project I've worked on for the past 5 years.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More