From 27b166c11c6b066a940efdbba3733e90554fd651 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Sun, 1 Oct 2023 10:44:28 +0330 Subject: [PATCH] CFE-5 initialize socket provider --- example.env.local | 1 + package.json | 1 + src/lib/app/contexts/socket.jsx | 14 ++++++++++++++ src/lib/app/hooks/useSocket.jsx | 10 ++++++++++ src/pages/_app.jsx | 31 +++++++++++++++++-------------- 5 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/lib/app/contexts/socket.jsx create mode 100644 src/lib/app/hooks/useSocket.jsx diff --git a/example.env.local b/example.env.local index 0be16f0..59d15b4 100644 --- a/example.env.local +++ b/example.env.local @@ -7,6 +7,7 @@ NEXT_PUBLIC_PRIMARY_MAIN = "#1b4332" NEXT_PUBLIC_SECONDARY_MAIN = "#800e13" NEXT_PUBLIC_BASE_URL = "https://crm.witel.ir" +NEXT_PUBLIC_SERVER_SOCKET_URL = "wss://crm.witel.ir" NEXT_PUBLIC_POWERED_BY_URL = "https://witel.ir" NODE_ENV = "development" \ No newline at end of file diff --git a/package.json b/package.json index 6e19bf6..5d6417a 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "react-dom": "^18.2.0", "react-toastify": "^9.1.3", "sass": "^1.62.0", + "socket.io-client": "^4.7.2", "stylis": "^4.1.3", "stylis-plugin-rtl": "^2.1.1", "swr": "^2.1.5", diff --git a/src/lib/app/contexts/socket.jsx b/src/lib/app/contexts/socket.jsx new file mode 100644 index 0000000..c21f27e --- /dev/null +++ b/src/lib/app/contexts/socket.jsx @@ -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 {children} +} \ No newline at end of file diff --git a/src/lib/app/hooks/useSocket.jsx b/src/lib/app/hooks/useSocket.jsx new file mode 100644 index 0000000..47d3290 --- /dev/null +++ b/src/lib/app/hooks/useSocket.jsx @@ -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 \ No newline at end of file diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index e76fbcb..fe249a6 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -7,24 +7,27 @@ import {UserProvider} from "@/lib/app/contexts/user"; import "moment/locale/fa"; import {NextIntlProvider} from "next-intl"; import TitlePage from "@/core/components/TitlePage"; +import {SocketProvider} from "@/lib/app/contexts/socket"; const App = ({Component, pageProps}) => { return (<> - - - - - {pageProps.messages ? : ''} - - - - - - - - - + + + + + + {pageProps.messages ? : ''} + + + + + + + + + + ) };