Skip to content

Commit

Permalink
Merge pull request #87 from masagal/send-token-as-set-auth-message
Browse files Browse the repository at this point in the history
send token as set auth websocket message
  • Loading branch information
sighmoan authored Aug 1, 2024
2 parents a3e4a6b + 99d099d commit 613e15f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ApiQueries/useWebSocketChat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { useEffect, useState } from "react";
import { Message } from "../utils/types";
import { useAuth } from "@clerk/clerk-react";

const useWebSocketChat = (defaultMessages: Message[]) => {
const [socket, setSocket] = useState<WebSocket>();
const [connectionEstablished, setConnectionEstablished] = useState<boolean>(false);
const [connectionEstablished, setConnectionEstablished] =
useState<boolean>(false);
const [messages, setMessages] = useState<Message[]>(defaultMessages);
const [pendingMessage, setPendingMessage] = useState<boolean>(false);
const auth = useAuth();

useEffect(() => {
const websocketUrl = import.meta.env.VITE_BACKEND_SOCKET_URL;
const socket = new WebSocket(websocketUrl);

socket.addEventListener("open", () => {
socket.addEventListener("open", async () => {
setConnectionEstablished(true);
setSocket(socket);
const token = await auth.getToken();
socket.send(`Set-Authorization ${token}`);
});

socket.addEventListener("message", (message) => {
Expand Down

0 comments on commit 613e15f

Please sign in to comment.