CFE-5 initialize socket provider
This commit is contained in:
14
src/lib/app/contexts/socket.jsx
Normal file
14
src/lib/app/contexts/socket.jsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import {createContext, useMemo} from "react";
|
||||
import {io} from "socket.io-client";
|
||||
|
||||
export const SocketContext = createContext()
|
||||
|
||||
export const SocketProvider = ({children}) => {
|
||||
const socket = useMemo(() => io(process.env.NEXT_PUBLIC_SERVER_SOCKET_URL, {
|
||||
autoConnect: false,
|
||||
auth: {
|
||||
token: ""
|
||||
}
|
||||
}), [])
|
||||
return <SocketContext.Provider value={{socket}}>{children}</SocketContext.Provider>
|
||||
}
|
||||
10
src/lib/app/hooks/useSocket.jsx
Normal file
10
src/lib/app/hooks/useSocket.jsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import {useContext} from "react";
|
||||
import {SocketContext} from "@/lib/app/contexts/socket";
|
||||
|
||||
const useSocket = () => {
|
||||
const {socket} = useContext(SocketContext)
|
||||
|
||||
return {socket}
|
||||
}
|
||||
|
||||
export default useSocket
|
||||
Reference in New Issue
Block a user