eplace/src/utils/streams.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2026-05-15 22:33:21 +02:00
import { io } from "socket.io-client";
// import { v4 as uuidv4 } from "uuid";
2026-05-15 11:08:23 +02:00
// FIXME: This file should handle the sockets and the subscriptions
// Exports must include
// - initSocket (initialize the connection to the socket server)
// - socket (variable resulting of initSocket function)
// Functions may include:
// - subscribe (subscribe to a room's stream or chat)
// - unsubscribe (unsubscribe from a room's stream or chat)
// - sendMessage (send a message to a room's chat)
2026-05-15 22:33:21 +02:00
let socket = null;
// let uuid = uuidv4();
/**
* Initializes the socket when authenticated
* returns {Promise<void>}
*/
async function initSocket() {
console.debug("Initializing socket connection");
const token = localStorage.getItem("token");
const host = import.meta.env.VITE_HOST;
const port = import.meta.env.VITE_PORT;
const wsUri = "ws://" + host + (port ? ":" + port : "");
socket = io(wsUri, {
reconnectionDelayMax: 10000,
extraHeaders: {
Authorization: "Bearer " + token,
},
});
console.debug("Socket active: " + socket.active);
return;
}
// TODO
// async function subscribe(room) {
// if (!room)
// room = "epi-place"
// console.warn("Skipping room susbscription (not implemented)")
// }
// async function unsubscribe() {
// }
// async function sendMessage() {
// }
export { initSocket, socket };