Skip to content

Commit

Permalink
Update socket URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mmli1 committed Jan 12, 2025
1 parent d3497a8 commit 8cddc1f
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions frontend/src/context/SocketContext.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import { createContext, useState, useEffect, useContext } from 'react';
import { useAuthContext } from './AuthContext';
import { createContext, useState, useEffect, useContext } from "react";
import { useAuthContext } from "./AuthContext";
import io from "socket.io-client";

const SocketContext = createContext();

export const useSocketContext = () => {
return useContext(SocketContext);
}
return useContext(SocketContext);
};

export const SocketContextProvider = ({children}) => {
const [socket, setSocket] = useState(null);
const [onlineUsers, setOnlineUsers] = useState([]);
const {authUser} = useAuthContext();
export const SocketContextProvider = ({ children }) => {
const [socket, setSocket] = useState(null);
const [onlineUsers, setOnlineUsers] = useState([]);
const { authUser } = useAuthContext();

useEffect(() => {
if(authUser) {
const socket = io('http://localhost:5000', {
query: {
userId: authUser._id
}
}
);
useEffect(() => {
if (authUser) {
const socket = io("https://chat-app-zwmw.onrender.com/", {
query: {
userId: authUser._id,
},
});

setSocket(socket);
//once we are online, see whos online and whos offline
//see socket.js comment for what socket.on does
socket.on("getOnlineUsers", (users) => {
setOnlineUsers(users);
})
setSocket(socket);
//once we are online, see whos online and whos offline
//see socket.js comment for what socket.on does
socket.on("getOnlineUsers", (users) => {
setOnlineUsers(users);
});

return () => socket.close();
} else {
if (socket) {
socket.close();
setSocket(null);
}
}
}, [authUser])
return () => socket.close();
} else {
if (socket) {
socket.close();
setSocket(null);
}
}
}, [authUser]);

return (
<SocketContext.Provider value={{socket, onlineUsers}}>{children}</SocketContext.Provider>
)
}
return (
<SocketContext.Provider value={{ socket, onlineUsers }}>
{children}
</SocketContext.Provider>
);
};

0 comments on commit 8cddc1f

Please sign in to comment.