При подключении библиотеки sockets.io возникает ошибка в файле main.ts
Выше видно и способ подключения.
Код main.ts
import {
utilities as nestWinstonModuleUtilities,
WinstonModule,
} from 'nest-winston';
import * as swStats from 'swagger-stats';
import * as winston from 'winston';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import AllExceptionsFilter from './exceptions-filters/all-exceptions.filter';
import BadRequestExceptionsFilter from './exceptions-filters/bad-request-exceptions.filter';
import * as Transport from 'winston-transport';
import { ConfigService } from '@nestjs/config';
import { ConfigParam } from './enums/config-params.enum';
import { IoAdapter } from "@nestjs/platform-socket.io";
async function bootstrap(): Promise<void> {
const transports: Transport[] = [];
if (process.env.NODE_ENV === 'production') {
transports.push(
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
winston.format.json()
),
level: 'debug'
})
);
} else {
transports.push(
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
nestWinstonModuleUtilities.format.nestLike()
),
level: 'debug'
})
);
}
const app = await NestFactory.create(AppModule, {
cors: true,
logger: WinstonModule.createLogger({
transports,
}),
});
const config = new DocumentBuilder()
.setTitle('API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('docs', app, document);
app.useGlobalFilters(
new AllExceptionsFilter(),
new BadRequestExceptionsFilter()
);
app.useGlobalPipes(
new ValidationPipe({ transform: true })
);
const configService = app.get(ConfigService);
app.setGlobalPrefix('api');
app.useWebSocketAdapter(new IoAdapter(app));
app.use(swStats.getMiddleware({ swaggerSpec: document }));
await app.listen(3000);
}
// app.useWebSocketAdapter(new WsAdapter(app))
// const redisIoAdapter = new RedisIoAdapter(app);
// await redisIoAdapter.connectToRedis();
// app.useWebSocketAdapter(redisIoAdapter);
bootstrap();
app.module.ts
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ControllersModule } from './controllers/controllers.module';
import { LoggerMiddleware } from './core/logging/logger.middleware';
import { DatabaseModule } from '@boilerplate/data';
import { ServicesModule } from './services/services.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import * as path from 'path';
import { IApiConfigParams } from './interfaces/api-config-params';
// import { EventEmitterModule } from '@nestjs/event-emitter';
import { SocketModule } from './socket/socket.module';
import { ScheduleModule } from '@nestjs/schedule';
const serveStatic = process.env.NX_SERVE_STATIC === 'true';
@Module({
imports: [
ConfigModule.forRoot({isGlobal: true}),
ServeStaticModule.forRoot({
rootPath: serveStatic ? path.resolve(process.env.NX_SERVE_STATIC_PATH || 'client') : null,
}),
DatabaseModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService<IApiConfigParams>) => ({
type: 'postgres',
host: configService.get('NX_DATABASE_HOST'),
port: +configService.get('NX_DATABASE_PORT'),
username: configService.get('NX_DATABASE_USERNAME'),
password: configService.get('NX_DATABASE_PASSWORD'),
database: configService.get('NX_DATABASE_NAME'),
logging: configService.get('NX_DATABASE_ENABLE_LOGGING') === 'true',
}),
inject: [ConfigService]
}),
ServicesModule,
ControllersModule,
SocketModule,
ScheduleModule.forRoot(),
]
})
export class AppModule {
public configure(consumer: MiddlewareConsumer): void {
consumer.apply(LoggerMiddleware)
.forRoutes('*');
}
}
package.json
{
"name": "api",
"version": "1.0.0",
"dependencies": {
"@nestjs/bull": "^0.6.1",
"@nestjs/common": "^8.2.5",
"@nestjs/config": "^1.1.6",
"@nestjs/core": "^8.2.5",
"@nestjs/jwt": "^8.0.0",
"@nestjs/passport": "^8.1.0",
"@nestjs/platform-express": "^8.2.5",
"@nestjs/platform-socket.io": "8.2.6",
"@nestjs/schedule": "^2.1.0",
"@nestjs/serve-static": "^2.2.2",
"@nestjs/swagger": "^5.1.5",
"@nestjs/terminus": "^8.0.3",
"@nestjs/typeorm": "^8.0.2",
"@nestjs/websockets": "8.2.6",
"@socket.io/redis-adapter": "7.1.0",
"@types/cron": "^2.0.0",
"bcrypt": "^5.0.1",
"bcryptjs": "^2.4.3",
"bull": "^4.8.5",
"nest-winston": "^1.6.2",
"passport": "^0.5.2",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"pg": "^8.7.1",
"prom-client": "^14.0.1",
"redis": "4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.2",
"socket.io": "4.5.0",
"supertest": "^6.2.1",
"swagger-stats": "^0.99.2",
"swagger-ui-express": "^4.3.0",
"typeorm": "^0.2.41",
"umzug": "^3.0.0",
"uuid": "^8.3.2",
"winston": "^3.4.0",
"winston-daily-rotate-file": "^4.5.5",
"ws": "7.4.5"
},
"devDependencies": {
"@nestjs/schematics": "^8.0.5",
"@nestjs/testing": "^8.2.5",
"@types/passport-local": "^1.0.34"
}
}
socket.module
import { forwardRef, Module } from '@nestjs/common';
import { SocketGateway } from './socket.gateway';
import { ServicesModule } from "../services/services.module";
import { ConfigModule } from "@nestjs/config";
@Module({
imports: [
forwardRef(() => ServicesModule),
ConfigModule
],
providers: [SocketGateway],
exports: [SocketGateway],
})
export class SocketModule {}
В чем может быть проблема? Скорее всего где то что то не так с версиями, или что то где то не экспортировал, хотя все проверенно очень плотно...
Код ошибки
(alias) new IoAdapter<any, any, any>(appOrHttpServer?: any): IoAdapter
import IoAdapter
Argument of type 'IoAdapter' is not assignable to parameter of type 'WebSocketAdapter<any, any, any>'.
Types of property 'bindMessageHandlers' are incompatible.
Types of parameters 'transform' and 'transform' are incompatible.
Call signature return types 'Observable<any>' and 'Observable<any>' are incompatible.
The types of 'operator.call' are incompatible between these types.
Type '(subscriber: import("//node_modules/rxjs/internal/Subscriber").Subscriber<any>, source: any) => import("/home//path//node_modules/rxjs/internal/types").TeardownLogic' is not assignable to type '(subscriber: import("/home//path//node_modules/@nestjs/typeorm/node_modules/rxjs/dist/types/internal/Subscriber").Subscriber<any>, source: any) => import("/home//path//node_modules/@nestjs/typeorm/node_modules/rxjs/dist/types/internal/types").TeardownLogic'.
Types of parameters 'subscriber' and 'subscriber' are incompatible.
Type 'Subscriber<any>' is missing the following properties from type 'Subscriber<any>': syncErrorValue, syncErrorThrown, syncErrorThrowable, _unsubscribeAndRecycle, and 2 more.ts(2345)