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.
22 lines (21 loc) • 726 B
JavaScript
// Hook for initializing and managing the socket client
import { useSocketContext } from "../context/SocketContext";
export const useSocketClient = () => {
const { socket, connected, setAuthToken } = useSocketContext();
const emitEvent = (event, data, callback) => {
if (socket === null || socket === void 0 ? void 0 : socket.connected) {
socket.emit(event, data, callback);
}
};
const reconnect = () => {
socket === null || socket === void 0 ? void 0 : socket.disconnect();
socket === null || socket === void 0 ? void 0 : socket.connect();
};
return {
socket,
connected,
emitEvent,
reconnect,
setAuthToken,
};
};