UNPKG

@rxap/nest-socket-io-client

Version:

This package provides a NestJS module for integrating a Socket.IO client. It offers a client proxy service and a provider for managing the Socket.IO client connection, allowing NestJS applications to easily interact with Socket.IO servers.

86 lines (85 loc) 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocketIoClientProvider = exports.DisconnectReason = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const config_1 = require("@nestjs/config"); const rxjs_1 = require("rxjs"); const socket_io_client_1 = require("socket.io-client"); var DisconnectReason; (function (DisconnectReason) { /** * The server has forcefully disconnected the socket with socket.disconnect() */ DisconnectReason["IO_SERVER_DISCONNECT"] = "io server disconnect"; /** * The socket was manually disconnected using socket.disconnect() */ DisconnectReason["IO_CLIENT_DISCONNECT"] = "io client disconnect"; /** * The server did not send a PING within the pingInterval + pingTimeout range */ DisconnectReason["PING_TIMEOUT"] = "ping timeout"; /** * The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G) */ DisconnectReason["TRANSPORT_CLOSE"] = "transport close"; /** * The connection has encountered an error (example: the server was killed during a HTTP long-polling cycle) */ DisconnectReason["TRANSPORT_ERROR"] = "transport error"; })(DisconnectReason || (exports.DisconnectReason = DisconnectReason = {})); let SocketIoClientProvider = class SocketIoClientProvider { constructor() { this.disconnected$ = new rxjs_1.Subject(); this.connected$ = new rxjs_1.Subject(); this.isConnected$ = new rxjs_1.BehaviorSubject(false); this.getSocket = () => { if (!this.socket) { return this.connect(); } return this.socket; }; } connect() { this.socket = (0, socket_io_client_1.io)(this.config.getOrThrow('COORDINATOR_SOCKET'), { transportOptions: { polling: { extraHeaders: { Authorization: `Bearer ${this.config.getOrThrow('COORDINATOR_JWT')}`, }, }, }, }); this.socket.on('disconnect', (reason) => { this.logger.error('Socket is disconnected', 'SocketIoClientProvider'); this.disconnected$.next(reason); this.isConnected$.next(false); if (reason == DisconnectReason.IO_SERVER_DISCONNECT) { this.logger.error('Server closed the connection. Try to reconnect', 'SocketIoClientProvider'); this.socket?.connect(); } }); this.socket.on('connect', () => { this.logger.log('Socket is connected', 'SocketIoClientProvider'); this.connected$.next(); this.isConnected$.next(true); }); this.socket.on('connect_error', () => { this.logger.error('Connection error', 'SocketIoClientProvider'); }); return this.socket; } }; exports.SocketIoClientProvider = SocketIoClientProvider; tslib_1.__decorate([ (0, common_1.Inject)(common_1.Logger), tslib_1.__metadata("design:type", common_1.Logger) ], SocketIoClientProvider.prototype, "logger", void 0); tslib_1.__decorate([ (0, common_1.Inject)(config_1.ConfigService), tslib_1.__metadata("design:type", config_1.ConfigService) ], SocketIoClientProvider.prototype, "config", void 0); exports.SocketIoClientProvider = SocketIoClientProvider = tslib_1.__decorate([ (0, common_1.Injectable)() ], SocketIoClientProvider);