jsm-core
Version:
Core library for JSM project
102 lines (101 loc) • 3.29 kB
TypeScript
import { JsmLogger } from 'jsm-logger';
import { Server as SocketIOServer, Socket } from 'socket.io';
type TSocketManagerStatus = 'disconnected' | 'connected' | 'connecting' | 'error';
export type TSocketEventData<P = any> = {
event: string;
payload: P;
header?: Record<string, any>;
socketId?: string;
timestamp?: string;
};
export type TNotificationRelatedTo = {
app?: string[] | string | null;
user?: string[] | string;
company?: string[] | string | null;
office?: string[] | string;
store?: string[] | string;
role?: string[] | string;
role_group?: string[] | string;
parcelStatus?: string[] | string;
account?: string[] | string;
} & {
[K: string]: string[] | string | null | undefined;
};
/**
* Format the notification space with the provided information.
* @param space The notification space string.
* example: "Notification for [COMPANY] - [APP] - [OFFICE] - [STORE] - [USER] - [ROLE] - [ROLE_GROUP] - [PARCEL_STATUS]"
* @param infos The information to replace in the space.
* @returns The formatted notification space.
*/
export declare const formatNotificationSpace: (infos: TNotificationRelatedTo) => string[];
export declare class SocketManager {
io: SocketIOServer;
logger: JsmLogger;
status: TSocketManagerStatus;
private clients;
/**
* Creates an instance of SocketManager.
*/
constructor();
/**
* Initialize socket.io server (called on app startup)
*/
init(): Promise<void>;
/**
* Register a client manually (not needed for socket.io, but kept for API compatibility)
*/
registerClient(clientId: string): void;
/**
* Unregister a client manually
*/
unregisterClient(clientId: string): void;
/**
* Emit an event to a specific socket if provided or all clients
* @param event The event name to emit
* @param data The data to send with the event
* @param socket Optional specific socket to emit to
* @returns void
*/
emit(event: string, data: any, socket?: Socket): void;
/**
* Listen for an event from clients
*/
on(event: string, handler: (...args: any[]) => void): void;
/**
* Register a handler for new client connections
*/
onConnection(handler: (socket: Socket) => void): void;
/**
* Register event handlers for all connected and future clients
*/
onClientEvent(event: string, handler: (socket: Socket, ...args: any[]) => void): void;
/**
* Get current connection status
*/
getStatus(): TSocketManagerStatus;
/**
* Get all connected clients
*/
getClients(): string[];
/**
* Notify specific rooms or all clients with contextual room formatting
*/
notify<T extends Record<string, any> = any>(event: string | string[], payload: T, relatedTo?: TNotificationRelatedTo): Promise<void>;
/**
* Get detailed connection statistics and room information
*/
getStats(): {
status: TSocketManagerStatus;
clientCount: number;
connectedClients: string[];
rooms: {
name: string;
clientCount: number;
clients: string[];
}[];
roomCount: number;
timestamp: string;
};
}
export {};