UNPKG

@huddle01/web-core

Version:

The Huddle01 Javascript SDK offers a comprehensive suite of methods and event listeners that allow for seamless real-time audio and video communication with minimal coding required.

499 lines (494 loc) 15.3 kB
import { EnhancedEventEmitter } from './common-js/EnhancedEventEmitter.cjs'; import RemotePeer from './RemotePeer.cjs'; import { NewPeerJoined } from './types/common.types.cjs'; import Bot, { VolatileDataMessage } from './Bot.cjs'; import { z } from 'zod'; import ActiveSpeakers from './ActiveSpeakers.cjs'; type CreateRoomData = { autoConsume?: boolean; activeSpeakers?: { size: number; }; volatileMessages?: boolean; }; type RoomEvents = { 'room-joined': []; 'room-joined-failed': [ data: { status: 'ROOM_NOT_FOUND' | 'ROOM_ERRORED'; message: string; } ]; /** * Emitted when the room is left */ 'room-closed': [ { reason: 'LEFT' | 'CLOSED' | 'KICKED' | 'DENIED' | 'CONNECTION_EXPIRED' | 'MAX_PEERS_REACHED' | 'ROOM_EXPIRED'; message?: string; } ]; 'room-connecting': []; 'new-peer-joined': [data: NewPeerJoined]; 'lobby-peers-updated': [peerIds: string[]]; 'new-lobby-peer': [{ peerId: string; metadata?: string; }]; 'metadata-updated': [data: { metadata: unknown; }]; 'peer-left': [ data: { peerId: string; role: string | null; metadata: unknown; } ]; 'room-controls-updated': []; 'room-notification': [data: { tag: string; message: string; code: number; }]; 'active-speakers-change': [ data: { peerIds: string[]; dominantSpeaker: string; } ]; 'received-volatile-data': [data: VolatileDataMessage]; 'room-role-updated': [ data: { peerId: string; newRole: string; prevRole: string; } ]; 'room-waiting': [ data: { reason: 'WAITING_FOR_PERMISSIONS' | 'WAITING_FOR_ROOM_TO_START' | 'WAITING_FOR_ADMIN_TO_JOIN'; message?: string; } ]; 'stream-added': [data: { peerId: string; label: string; }]; 'stream-closed': [data: { peerId: string; label: string; }]; }; type RoomStates = 'idle' | 'connecting' | 'connected' | 'failed' | 'left' | 'closed'; type RoomStats = { startTime: number; }; declare class Room extends EnhancedEventEmitter<RoomEvents> { /** * Room Instance, Singleton class */ private static __instance; /** * Socket Instance, Singleton class */ private __socket; /** * Returns the instance of the socket connection */ private get socket(); /** * Room Id of the current room */ private __roomId; /** * session id */ private __sessionId; /** * Lobby PeerIds */ private __lobbyPeers; /** * Removed Lobby PeerId from the lobby * @param peerId - PeerId of the peer who joined the room */ private removeLobbyPeer; /** * Room Config Object * - `allowProduce`: Allow non-admin Peers in the Room to produce Media Streams * - `allowConsume`: Allow non-admin Peers in the Room to consume Media Streams * - `allowSendData`: Allow non-admin Peers in the Room to send data message * - `roomLocked`: If the room is locked */ private __config; /** * Auto consume flag, if set to true, Peers Joining the Room will automatically consume the media streams of the remote peers * * @default true * * @remarks - This flag is used by the `useRoom` hook to automatically consume the media streams of the remote peers, * - if set to false, the user will have to manually consume the media streams of the remote peers * using the `consume` method of the `LocalPeer` instance `localPeer.consume({ peerId, label: "video", appData: {} });` */ readonly autoConsume: boolean; /** * Default Active Speaker Size, if not provided in the room config object * @default 0 * @remarks - To update the active speaker size, you can use function `room.activeSpeakers.updateSize(size: number)` */ private __defaultActiveSpeakerSize; /** * Get the default active speaker size which can be updated using option `activeSpeaker` when initializing the HuddleClient. * @remarks - To update the active speaker while inside the meeting, use the function `room.activeSpeakers.updateSize(size: number)` */ get defaultActiveSpeakerSize(): number; /** * Handler for the ActiveSpeaker */ private __activeSpeakers; /** * Get the active speakers instance */ get activeSpeakers(): ActiveSpeakers | null; set activeSpeakers(activeSpeakers: ActiveSpeakers); /** * Bot Instance, which is used to manage Volatile Messages inside the SDK. */ private __bot; get bot(): Bot | null; set bot(bot: Bot); /** * State of the Room */ private __state; /** * Room Stats */ private __stats; /** * Set the state of the room */ set state(newState: RoomStates); /** * State of the room */ get state(): RoomStates; /** * Get the lobby peers in the form of map * @returns - Map of Lobby PeerIds, with the metadata, {peerId ==> {peerId, metadata}} * @example * ```ts * const lobbyPeers = room.lobbyPeersMap; * * for (const [peerId, metadata] of lobbyPeers) {} * * ``` */ get lobbyPeersMap(): Map<string, { peerId: string; metadata?: string; }>; /** * Get lobby peers in the form of array */ get lobbyPeerIds(): string[]; /** * Get lobby peers in the form of array * @returns - Array of Lobby PeerIds */ get lobbyPeers(): Map<string, { peerId: string; metadata?: string; }>; /** * Set lobby peers in the form of map * `NOTE: This function is an internal function of the SDK, Used to emit evnts based on changes` */ set lobbyPeersMap(peers: Map<string, { peerId: string; metadata?: string; }>); /** * Get Room Stats */ get stats(): RoomStats; /** * Set Room Stats */ set stats(stats: RoomStats); /** * Get the lobby peer metadata * @param peerId - PeerId of the Lobby Peer * @returns - Lobby Peer Metadata */ getLobbyPeerMetadata: <T = unknown>(peerId: string) => { peerId: string; metadata: T; }; /** * Add Lobby Peers to the room, this will add the lobby peers to the existing lobby peers */ _addLobbyPeers: (peers: { peerId: string; metadata?: string; }[]) => void; /** * Set the new lobby peers, this will clear the existing lobby peers and add the new lobby peers * @param peers - Array of Lobby Peers */ _setLobbyPeers(peers: { peerId: string; metadata?: string; }[]): void; /** * Room Config Object * - `allowProduce`: Allow non-admin Peers in the Room to produce Media Streams * - `allowConsume`: Allow non-admin Peers in the Room to consume Media Streams * - `allowSendData`: Allow non-admin Peers in the Room to send data message * - `roomLocked`: If the room is locked */ get config(): RoomInfo['config']; set config(config: RoomInfo['config']); /** * Remote Peers Map, Stores all the remote peers */ remotePeers: Map<string, RemotePeer>; /** * Metadata of the room. */ private __metadata; /** * Setter function for the metadata of the room * `Note: This will not update the metadata of the room, this is just a setter function for the metadata` * `To notify everyone in the room about the metadata change, use the updateMetadata function` */ set metadata(metadata: string); /** * Get the metadata of the room */ getMetadata: <T = unknown>() => T; /** * Update Metadata of the room * @throws { Error } If Request Failed to Update Metadata */ updateMetadata: <T = unknown>(data: T) => Promise<void>; /** * Create a new Room Instance if not created, else return the existing Room Instance * * @returns - Room Instance */ static create(data?: CreateRoomData): Room; /** * Get the Room Instance if its not initialized it will throw an error * @returns - Room Instance * @throws { Error } If the Room Instance is not initialized */ static getInstance: () => Room; /** * RoomId of the currently joined room. */ get roomId(): string | null; /** * */ get sessionId(): string | null; set roomId(roomId: string); set sessionId(sessionId: string); /** * Returns the PeerIds of the remote peers */ get peerIds(): string[]; /** * @description Update room control booleans - roomLocked, allowProduce, allowConsume, allowSendData * @param data: TNewRoomControls * @throws { Error } If the Peer Calling this Functions is not an admin, then the permissions will not be updated */ updateRoomControls: (data: NewRoomControls) => Promise<void>; /** * Close a particular stream of remote peers * @param data: { label: string; peerIds?: string[] } * @param label: Label of the stream * @param peerIds: PeerIds of the remote peers, if not provided, it will close the stream of all the remote peers * @throws { Error } If the Peer Calling this Function is not an admin, then the permissions will not be updated */ closeStreamOfLabel: (data: { label: string; peerIds?: string[]; }) => Promise<void>; /** * Mute everyone in the room. This will close the audio stream of all the remote peers who dont have admin permissions * * `NOTE: This will target all the audio stream in the room with the label "audio"` * * @throws { Error } If the Peer Calling this Function is not an admin, then the permissions will not be updated */ muteEveryone: () => Promise<void>; /** * Returns the Remote Peer with the given peerId is present in the room. Returns null if the peer is not present in the room. * * @param peerId - PeerId of the remote peer * @returns - RemotePeer Instance * @return - null if the peer is not present in the room */ remotePeerExists: (peerId: string) => RemotePeer | null; /** * Returns the Remote Peer if present in room. * @param peerId - PeerId of the remote peer * @returns - RemotePeer Instance * @throws { Error } If the Remote Peer is not found */ getRemotePeerById(peerId: string): RemotePeer; private constructor(); /** * Connects to the room and returns the instance of the room, * * @param data - roomId to connect to, make sure the roomId matches the roomId in the token used to connect the socket, else it will throw an error * @throws { Error } If the socket connection is not connected, or if the socket connection is connecting */ connect: (data: { roomId: string; }) => Room; /** * Admit a Peer to the room who is in the lobby * * @throws {Error} If the Peer Calling this Function is not an admin, then the permissions will not be updated */ admitPeer: (peerId: string) => Promise<void>; /** * Denies the peer from joining the room, who is in the lobby */ denyPeer: (peerId: string) => Promise<void>; /** * kick peer from room with respective peerId * @throws { Error } If the Peer Calling this Function is not an admin, then the permissions will not be updated */ kickPeer: (peerId: string) => Promise<void>; /** * closing the room for the current user, room will keep on running for the remote users */ close: (reason?: "LEFT" | "CLOSED" | "KICKED" | "DENIED" | "CONNECTION_EXPIRED" | "MAX_PEERS_REACHED" | "ROOM_EXPIRED") => void; } declare const RoomControlsSchema: z.ZodObject<{ roomLocked: z.ZodBoolean; allowProduce: z.ZodBoolean; allowProduceSources: z.ZodObject<{ cam: z.ZodDefault<z.ZodBoolean>; mic: z.ZodDefault<z.ZodBoolean>; screen: z.ZodDefault<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { cam: boolean; mic: boolean; screen: boolean; }, { cam?: boolean | undefined; mic?: boolean | undefined; screen?: boolean | undefined; }>; allowConsume: z.ZodBoolean; allowSendData: z.ZodBoolean; }, "strip", z.ZodTypeAny, { roomLocked: boolean; allowProduce: boolean; allowProduceSources: { cam: boolean; mic: boolean; screen: boolean; }; allowConsume: boolean; allowSendData: boolean; }, { roomLocked: boolean; allowProduce: boolean; allowProduceSources: { cam?: boolean | undefined; mic?: boolean | undefined; screen?: boolean | undefined; }; allowConsume: boolean; allowSendData: boolean; }>; declare const PermissionSchema: z.ZodObject<{ admin: z.ZodBoolean; canConsume: z.ZodBoolean; canProduce: z.ZodBoolean; canProduceSources: z.ZodObject<{ cam: z.ZodBoolean; mic: z.ZodBoolean; screen: z.ZodBoolean; }, "strip", z.ZodTypeAny, { cam: boolean; mic: boolean; screen: boolean; }, { cam: boolean; mic: boolean; screen: boolean; }>; canSendData: z.ZodBoolean; canRecvData: z.ZodBoolean; canUpdateMetadata: z.ZodBoolean; }, "strip", z.ZodTypeAny, { admin: boolean; canConsume: boolean; canProduce: boolean; canProduceSources: { cam: boolean; mic: boolean; screen: boolean; }; canSendData: boolean; canRecvData: boolean; canUpdateMetadata: boolean; }, { admin: boolean; canConsume: boolean; canProduce: boolean; canProduceSources: { cam: boolean; mic: boolean; screen: boolean; }; canSendData: boolean; canRecvData: boolean; canUpdateMetadata: boolean; }>; type TProducerInfo = { id: string; label: string; paused: boolean; }[]; type ProduceSources = RoomControls['allowProduceSources']; type RoomInfo = { roomLocked: boolean; config: RoomControls; options?: { maxPeersAllowed?: number; }; metadata?: string; peers: { peerId: string; producers: TProducerInfo; metadata?: string; role?: string; }[]; lobbyPeers: { peerId: string; metadata?: string; }[]; startTime: number; }; type RoomControls = z.infer<typeof RoomControlsSchema>; type NewRoomControls = { value: boolean; type: 'roomLocked' | 'allowProduce' | 'allowConsume' | 'allowSendData'; } | { value: { cam: boolean; mic: boolean; screen: boolean; }; type: 'allowProduceSources'; }; type TPermissions = z.infer<typeof PermissionSchema>; export { type NewRoomControls as N, type ProduceSources as P, Room as R, type TPermissions as T, type RoomEvents as a, type RoomStates as b, type RoomInfo as c, type RoomControls as d, type RoomStats as e };