@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.
46 lines (45 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketIoClientStrategy = void 0;
const microservices_1 = require("@nestjs/microservices");
/**
* SocketIoClientStrategy is a custom transport strategy that allows
* a Socket.IO client to communicate with the server. It extends the Server class
* and implements the CustomTransportStrategy interface.
*/
class SocketIoClientStrategy extends microservices_1.Server {
constructor(client, logger) {
super();
this.client = client;
this.logger = logger;
}
/**
* This method is triggered when you run "app.listen()".
*/
listen(callback) {
this.client.on('connection', () => {
this.logger.log('connection', 'SocketIoClientStrategy');
});
this.client.on('error', (error) => {
if (error && typeof error === 'object' && error.message) {
this.logger.error(error.message, 'SocketIoClientStrategy');
}
else {
this.logger.error(error);
}
});
this.messageHandlers.forEach((handler, pattern) => {
this.client.on(pattern, (data) => {
handler(data, this.client);
});
});
callback();
}
/**
* This method is triggered on application shutdown.
*/
close() {
this.client.disconnect();
}
}
exports.SocketIoClientStrategy = SocketIoClientStrategy;