Skip to content

Commit

Permalink
feat(socket): create socket.io connection b/w server for real time chat
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunan-k committed Nov 7, 2023
1 parent 5620955 commit c755a97
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 48 deletions.
12 changes: 12 additions & 0 deletions app/components/SingleChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import { SERVER_URL } from "@/utils/global";
import { ChatState } from "@/contexts/ChatProvider";
import ScrollableChat from "./ScrollableChat";
import { MessageData } from "@/types";
import io, { Socket } from "socket.io-client";

const ENDPOINT = SERVER_URL;
let socket: Socket;

const SingleChat = () => {
const { isDark } = useContext(DarkLightModeContext)!;
const { user, selectedChat } = ChatState()!;
const [messages, setMessages] = useState<MessageData[]>([]);
const [loading, setLoading] = useState(false);
const [newMessage, setNewMessage] = useState("");
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
const [socketConnected, setSocketConnected] = useState(false);

const sendMessage = async (event: React.KeyboardEvent<HTMLElement>) => {
if (event.key === "Enter" && newMessage) {
Expand Down Expand Up @@ -70,6 +76,12 @@ const SingleChat = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedChat]);

useEffect(() => {
socket = io(ENDPOINT);
socket.emit("setup", user);
socket.on("connection", () => setSocketConnected(true));
}, [user]);

return (
<>
{loading ? (
Expand Down
Loading

0 comments on commit c755a97

Please sign in to comment.