socket.io-react-hooks-advanced
Version:
A modular and extensible React + Socket.IO hook library designed for real-world applications. Supports namespaced sockets, reconnection strategies, offline queues, latency monitoring, middleware, encryption, and more.
26 lines (21 loc) • 625 B
text/typescript
// Hook for initializing and managing the socket client
import {useSocketContext} from "../context/SocketContext";
export const useSocketClient = () => {
const {socket, connected, setAuthToken} = useSocketContext();
const emitEvent = (event: string, data: any, callback?: (...args: any[]) => void) => {
if (socket?.connected) {
socket.emit(event, data, callback);
}
};
const reconnect = () => {
socket?.disconnect();
socket?.connect();
};
return {
socket,
connected,
emitEvent,
reconnect,
setAuthToken,
};
};