UNPKG

atozas-push-notification

Version:

Real-time push notifications across platforms using socket.io

274 lines 8.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AtozasPushNotificationClient = void 0; const socket_io_client_1 = require("socket.io-client"); const notification_display_1 = require("./notification-display"); class AtozasPushNotificationClient { constructor(config) { this.socket = null; this.userInfo = null; this.isConnected = false; this.reconnectAttempts = 0; // Event handlers this.onNotificationCallback = null; this.onConnectionCallback = null; this.onErrorCallback = null; this.onUserStatusCallback = null; this.config = { autoConnect: true, reconnection: true, reconnectionAttempts: 5, reconnectionDelay: 1000, reconnectionDelayMax: 5000, timeout: 20000, forceNew: false, requestPermission: true, fallbackToUI: true, showNotifications: true, enableVibration: true, ...config }; // Initialize notification display manager this.displayManager = new notification_display_1.NotificationDisplayManager(this.config); if (this.config.autoConnect) { this.connect(); } } /** * Connect to the notification server */ connect() { return new Promise((resolve, reject) => { try { this.socket = (0, socket_io_client_1.io)(this.config.url, { autoConnect: false, reconnection: this.config.reconnection, reconnectionAttempts: this.config.reconnectionAttempts, reconnectionDelay: this.config.reconnectionDelay, reconnectionDelayMax: this.config.reconnectionDelayMax, timeout: this.config.timeout, forceNew: this.config.forceNew }); this.setupEventHandlers(); this.socket.connect(); this.socket.on('connect', () => { this.isConnected = true; this.reconnectAttempts = 0; // Re-authenticate if user info exists if (this.userInfo) { this.authenticate(this.userInfo); } if (this.onConnectionCallback) { this.onConnectionCallback(true); } resolve(); }); this.socket.on('connect_error', (error) => { this.isConnected = false; if (this.onErrorCallback) { this.onErrorCallback(error); } reject(error); }); } catch (error) { reject(error); } }); } /** * Disconnect from the notification server */ disconnect() { if (this.socket) { this.socket.disconnect(); this.isConnected = false; if (this.onConnectionCallback) { this.onConnectionCallback(false); } } } /** * Authenticate user with the server */ authenticate(userInfo) { this.userInfo = userInfo; if (this.socket && this.isConnected) { this.socket.emit('authenticate', userInfo); } } /** * Join a group for group notifications */ joinGroup(groupId) { if (this.socket && this.isConnected) { this.socket.emit('join_group', groupId); } } /** * Leave a group */ leaveGroup(groupId) { if (this.socket && this.isConnected) { this.socket.emit('leave_group', groupId); } } /** * Set notification callback handler */ onNotification(callback) { this.onNotificationCallback = callback; } /** * Set connection status callback handler */ onConnection(callback) { this.onConnectionCallback = callback; } /** * Set error callback handler */ onError(callback) { this.onErrorCallback = callback; } /** * Set user status callback handler */ onUserStatus(callback) { this.onUserStatusCallback = callback; } /** * Get connection status */ getConnectionStatus() { return this.isConnected; } /** * Get current user info */ getUserInfo() { return this.userInfo; } /** * Send acknowledgment for received notification */ acknowledgeNotification(notificationId) { if (this.socket && this.isConnected) { this.socket.emit('notification_ack', notificationId); } } /** * Request user status */ requestUserStatus(userId) { if (this.socket && this.isConnected) { this.socket.emit('request_user_status', userId); } } /** * Request notification permission */ async requestNotificationPermission() { return await this.displayManager.requestPermission(); } /** * Get notification permission status */ getNotificationPermission() { return this.displayManager.getPermissionStatus(); } /** * Set notification click handler */ onNotificationClick(callback) { this.displayManager.onNotificationClick(callback); } /** * Close all active notifications */ closeAllNotifications() { this.displayManager.closeAllNotifications(); } /** * Display a local notification (without sending through socket) */ displayLocalNotification(notification, options) { this.displayManager.displayNotification(notification, options); } /** * Setup socket event handlers */ setupEventHandlers() { if (!this.socket) return; // Handle notifications this.socket.on('notification', (data) => { // Display notification visually if (this.config.showNotifications) { this.displayManager.displayNotification(data.notification, data.options); } // Call user callback if (this.onNotificationCallback) { this.onNotificationCallback(data.notification, data.options); } }); // Handle disconnection this.socket.on('disconnect', (reason) => { this.isConnected = false; if (this.onConnectionCallback) { this.onConnectionCallback(false); } // Auto-reconnect logic if (this.config.reconnection && reason === 'io server disconnect') { this.handleReconnection(); } }); // Handle reconnection this.socket.on('reconnect', () => { this.isConnected = true; this.reconnectAttempts = 0; // Re-authenticate if (this.userInfo) { this.authenticate(this.userInfo); } if (this.onConnectionCallback) { this.onConnectionCallback(true); } }); // Handle reconnection attempts this.socket.on('reconnect_attempt', () => { this.reconnectAttempts++; }); // Handle user status updates this.socket.on('user_status', (data) => { if (this.onUserStatusCallback) { this.onUserStatusCallback(data.userId, data.online); } }); // Handle errors this.socket.on('error', (error) => { if (this.onErrorCallback) { this.onErrorCallback(error); } }); } /** * Handle reconnection logic */ handleReconnection() { if (this.reconnectAttempts >= (this.config.reconnectionAttempts || 5)) { if (this.onErrorCallback) { this.onErrorCallback(new Error('Max reconnection attempts reached')); } return; } const delay = Math.min(this.config.reconnectionDelay * Math.pow(2, this.reconnectAttempts), this.config.reconnectionDelayMax); setTimeout(() => { if (!this.isConnected && this.socket) { this.socket.connect(); } }, delay); } } exports.AtozasPushNotificationClient = AtozasPushNotificationClient; //# sourceMappingURL=client.js.map