CFE-5 initialize socket provider

This commit is contained in:
AmirHossein Mahmoodi
2023-10-01 10:44:28 +03:30
parent d22650c176
commit 27b166c11c
5 changed files with 43 additions and 14 deletions

View 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>
}

View 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