Real-time notifications are a core feature in many modern applications.
Recently, while working on a Vue + Laravel application, I faced multiple issues with Socket.IO-based notifications that worked fine locally but caused problems in production.
In this post, I’ll explain what went wrong, how I fixed it, and the lessons I learned.
🔔 The Problem
We implemented real-time notifications using Socket.IO so users could instantly receive updates (events, alerts, system messages).
Issues we faced:
- Notifications randomly stopped arriving
- Duplicate notifications after page refresh
- Users stayed “connected” but didn’t receive messages
- Everything worked locally, but production was unstable
This caused confusion and reduced trust in the system.
🧰 Tech Stack
- Frontend: Vue.js
- Backend: Laravel
- WebSocket: Socket.IO
- Auth: Token-based authentication
- Server: Nginx + Production environment
🔍 Root Cause Analysis
After debugging both frontend and backend, the main issues were:
- WebSocket connections were created multiple times
- Event listeners were not cleaned up
- Users were not mapped correctly to socket connections
- Idle connections were silently dropped
- Reconnection logic caused duplicate events
In short:
👉 The WebSocket connection existed, but it wasn’t managed properly.
🛠️ The Solution
1️⃣ Proper Socket Initialization in Vue
The socket was being re-initialized on every component load.
Fix: Create a single reusable socket instance.
js
// socket.js
import { io } from "socket.io-client";
let socket = null;
export const connectSocket = (token) => {
if (!socket) {
socket = io(import.meta.env.VITE_SOCKET_URL, {
auth: { token },
transports: ["websocket"],
});
}
return socket;
};
Top comments (0)