Files
group-2-threaded-project-pe…/web/lib/chatSocket.js

26 lines
733 B
JavaScript

/*
* Creates a STOMP WebSocket client for the live chat feature.
*
* Author: Shiv
* Date: April 2026
*/
import { Client } from "@stomp/stompjs";
//Backend URL for the WebSocket connection, empty string means same origin
const BACKEND_URL = process.env.NEXT_PUBLIC_BACKEND_URL || "";
//Creates a STOMP client that connects to the chat WebSocket with the user's token
export function createStompClient(token) {
return new Client({
webSocketFactory: () => {
const SockJS = require("sockjs-client");
return new SockJS(`${BACKEND_URL}/ws/chat-sockjs`);
},
connectHeaders: { Authorization: `Bearer ${token}` },
reconnectDelay: 5000,
heartbeatIncoming: 10000,
heartbeatOutgoing: 10000,
});
}