Skip to content

Commit

Permalink
fix(*): cors workaround socket adapter (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Aug 2, 2021
1 parent 373bed7 commit a462f08
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as helmet from 'helmet';
import { LogLevel } from '@nestjs/common';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { ServerOptions } from 'socket.io';

/**
* https://stackoverflow.com/questions/65957386/cors-error-with-socket-io-connections-on-chrome-v88-and-nestjs-server
*/
export class WorkaroundSocketAdapter extends IoAdapter {
createIOServer(
port: number,
options?: ServerOptions & { namespace?: string; server?: any },
) {
return super.createIOServer(port, { ...options, cors: true });
}
}

async function bootstrap() {
let logLevels: LogLevel[];
Expand All @@ -16,6 +30,7 @@ async function bootstrap() {
logger: logLevels,
});

app.useWebSocketAdapter(new WorkaroundSocketAdapter(app));
app.enableCors();
app.use(helmet());
app.use(
Expand Down

0 comments on commit a462f08

Please sign in to comment.