diff --git a/src/fireedge/src/client/hooks/useSocket.js b/src/fireedge/src/client/hooks/useSocket.js index ed6506de0f..c63a3b7b90 100644 --- a/src/fireedge/src/client/hooks/useSocket.js +++ b/src/fireedge/src/client/hooks/useSocket.js @@ -1,11 +1,36 @@ import { useContext, useCallback } from 'react' +import socketIO from 'socket.io-client' -import { SOCKETS } from 'client/constants' +import { APP_URL, SOCKETS } from 'client/constants' import { SocketContext } from 'client/providers/socketProvider' +import { useAuth } from 'client/features/Auth' +import { useGeneral } from 'client/features/General' + +const createWebsocket = (path, query) => socketIO({ path: `${APP_URL}/${path}`, query }) + export default function useSocket () { + const { jwt } = useAuth() + const { zone } = useGeneral() + const { socket, isConnected } = useContext(SocketContext) + const getHooksSocketTemporal = useCallback(({ resource, id }) => { + const socket = createWebsocket( + SOCKETS.hooks, + { token: jwt, zone, resource, id } + ) + + return { + connect: callback => { + socket.on(SOCKETS.hooks, callback) + }, + disconnect: () => { + socket.disconnect() + } + } + }, [jwt, zone]) + const getHooksSocket = useCallback(func => ({ on: () => isConnected && socket.on(SOCKETS.hooks, func), off: () => isConnected && socket.off(SOCKETS.hooks, func) @@ -16,5 +41,10 @@ export default function useSocket () { off: () => isConnected && socket.off(SOCKETS.provision, func) }), [socket, isConnected]) - return { isConnected, getHooksSocket, getProvisionSocket } + return { + isConnected, + getHooksSocket, + getProvisionSocket, + getHooksSocketTemporal + } }