Skip to content

Commit b6dcd5b

Browse files
committed
Add socket connection status event listeners
Introduced onConnect, onDisconnect, and onConnectError functions to allow subscribing to socket connection status events. This enhances the ability to handle connection lifecycle events in the chat feature.
1 parent f8c0895 commit b6dcd5b

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

lib/features/chat/socket.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const initSocket = () => {
2121
console.error('Socket.io connection error:', error)
2222
})
2323
}
24-
24+
2525
return socket
2626
}
2727

@@ -167,6 +167,28 @@ export const onUserLeftRoom = (callback: (data: { userId: string; email: string;
167167
}
168168
}
169169

170+
// Connection status listeners
171+
export const onConnect = (callback: () => void) => {
172+
const sock = getSocket()
173+
if (sock) {
174+
sock.on('connect', callback)
175+
}
176+
}
177+
178+
export const onDisconnect = (callback: () => void) => {
179+
const sock = getSocket()
180+
if (sock) {
181+
sock.on('disconnect', callback)
182+
}
183+
}
184+
185+
export const onConnectError = (callback: (error: Error) => void) => {
186+
const sock = getSocket()
187+
if (sock) {
188+
sock.on('connect_error', callback)
189+
}
190+
}
191+
170192
// Cleanup function to remove all listeners
171193
export const removeAllListeners = () => {
172194
const sock = getSocket()

0 commit comments

Comments
 (0)