-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cecf505
commit 66a6766
Showing
14 changed files
with
241 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Server } from "socket.io"; | ||
import http from "http"; | ||
import express from "express"; | ||
|
||
const app = express(); | ||
|
||
const server = http.createServer(app); | ||
const io = new Server(server, { | ||
cors: { | ||
origin: ["http://localhost:3000"], | ||
methods: ["GET", "POST"], | ||
}, | ||
}); | ||
|
||
export const getReceiverSocketId = (receiverId) => { | ||
return userSocketMap[receiverId]; | ||
}; | ||
|
||
const userSocketMap = {}; // {userId: socketId} | ||
|
||
io.on("connection", (socket) => { | ||
console.log("a user connected", socket.id); | ||
|
||
const userId = socket.handshake.query.userId; | ||
if (userId != "undefined") userSocketMap[userId] = socket.id; | ||
|
||
// io.emit() is used to send events to all the connected clients | ||
io.emit("getOnlineUsers", Object.keys(userSocketMap)); | ||
|
||
// socket.on() is used to listen to the events. can be used both on client and server side | ||
socket.on("disconnect", () => { | ||
console.log("user disconnected", socket.id); | ||
delete userSocketMap[userId]; | ||
io.emit("getOnlineUsers", Object.keys(userSocketMap)); | ||
}); | ||
}); | ||
|
||
export { app, io, server }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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); | ||
}; | ||
|
||
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, | ||
}, | ||
}); | ||
|
||
setSocket(socket); | ||
|
||
// socket.on() is used to listen to the events. can be used both on client and server side | ||
socket.on("getOnlineUsers", (users) => { | ||
setOnlineUsers(users); | ||
}); | ||
|
||
return () => socket.close(); | ||
} else { | ||
if (socket) { | ||
socket.close(); | ||
setSocket(null); | ||
} | ||
} | ||
}, [authUser]); | ||
|
||
return <SocketContext.Provider value={{ socket, onlineUsers }}>{children}</SocketContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect } from "react"; | ||
|
||
import { useSocketContext } from "../context/SocketContext"; | ||
import useConversation from "../zustand/useConversation"; | ||
|
||
import notificationSound from "../assets/sounds/notification.mp3"; | ||
|
||
const useListenMessages = () => { | ||
const { socket } = useSocketContext(); | ||
const { messages, setMessages } = useConversation(); | ||
|
||
useEffect(() => { | ||
socket?.on("newMessage", (newMessage) => { | ||
newMessage.shouldShake = true; | ||
const sound = new Audio(notificationSound); | ||
sound.play(); | ||
setMessages([...messages, newMessage]); | ||
}); | ||
|
||
return () => socket?.off("newMessage"); | ||
}, [socket, setMessages, messages]); | ||
}; | ||
export default useListenMessages; |
Oops, something went wrong.