import { CompatClient, Message } from '@stomp/stompjs'; import LoginResult from '../models/LoginResult'; import { SocketEventListener } from './socket-events'; export interface ApplozicSocketOptions { /** Applozic AppID */ applicationId: string; /** Result from login API */ loginResult: LoginResult; /** Callbacks for realtime events like new message, typing status etc. */ events?: SocketEventListener[]; } /** * This class handles the socket connection to the applozic server. */ export default class ApplozicSocket { /** Internal stomp client */ private stompClient; /** WebSocket for the stomp client */ private webSocket; /** Promise to check if connection is initialized */ connectionPromise: Promise; /** Resolves initialization promise outside the initialization context */ private connectionPromiseResolver; /** Applozic Application ID */ private applicationId; /** User login result */ private loginResult; /** Socket event handlers */ private eventHandlers; constructor(options: ApplozicSocketOptions); private getWebSocket; private getStompClient; private onMessage; /** * Start connection to WebSocket server * * @returns Promise that resolves when connection is established */ connect: () => Promise; private onConnect; /** * Subscribe to a topic * @param topic Topic name * @param callback Function to run on new message in topic */ subscribe: (topic: string, callback: (stompMessage: Message) => void) => Promise; /** * Unsubscribe from a topic * @param topic Topic name */ unSubscribe: (topic: string) => Promise; private onError; private onClose; /** * Set current logged in user online status * @param isOnline True if user is online, false otherwise */ sendStatus: (isOnline: boolean) => void; /** Disconnect from the websocket */ disconnect: () => Promise; /** * Send a message to the server via websocket * * @param topic WebSocket topic * @param body message content */ sendMessage: (topic: string, body: string) => Promise; /** * Add a new event listener * @param handler Event listener */ addEventListener: (handler: SocketEventListener) => void; setEventListeners: (handlers: SocketEventListener[]) => void; }