easy3-5 min58% solve rate
The Event Listener Memory Leak
This WebSocket handler looks clean but will slowly consume all your memory.
Node.jsMemory
The Code
This code passed code review and works in development. But it has a bug that will cause problems in production.
websocket.js
1const WebSocket = require('ws');2const EventEmitter = require('events');34const notifications = new EventEmitter();56const wss = new WebSocket.Server({ port: 8080 });78wss.on('connection', (ws) => {9console.log('Client connected');1011// Listen for notifications12const handler = (data) => {13ws.send(JSON.stringify(data));14};1516notifications.on('new-notification', handler);1718ws.on('message', (message) => {19console.log('Received:', message);20});2122ws.on('close', () => {23console.log('Client disconnected');24});25});2627// This WebSocket server has a memory leak.28// It will grow unbounded over time.29// What's missing?
Need a Hint?
0 of 3 revealedHint 1 locked
Hint 2 locked
Hint 3 locked
Ready for the Solution?
Enter your email to get the full solution with explanation, the fix, and why this bug is so common.