Skip to content

Commit

Permalink
Merge pull request #5435 from novuhq/socket_io_admin
Browse files Browse the repository at this point in the history
feat(ws): Mount Socket.io Admin UI
  • Loading branch information
SokratisVidros authored Apr 24, 2024
2 parents 7643dd2 + fddc837 commit d55b1f2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,12 @@
"angular.json",
"ng-package.json",
"libs/shared/src/types/timezones/timezones.types.ts",
"*.riv"
"*.riv",
"websockets",
".env",
".env.development",
".env.local",
".env.production",
".env.test",
]
}
2 changes: 1 addition & 1 deletion .source
2 changes: 2 additions & 0 deletions apps/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/jwt": "10.2.0",
"@nestjs/platform-express": "^10.2.2",
"@nestjs/platform-socket.io": "^10.2.2",
"@nestjs/serve-static": "^4.0.2",
"@nestjs/swagger": "^7.1.9",
"@nestjs/terminus": "^10.0.1",
"@nestjs/websockets": "^10.2.2",
Expand All @@ -33,6 +34,7 @@
"@novu/shared": "^0.24.1",
"@novu/testing": "^0.24.1",
"@sentry/node": "^7.30.0",
"@socket.io/admin-ui": "^0.5.1",
"@socket.io/redis-adapter": "^7.2.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
4 changes: 4 additions & 0 deletions apps/ws/src/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true
LOGGING_LEVEL=info

AUTO_CREATE_INDEXES=true

SOCKET_IO_ADMIN_USERNAME=admin
# "changeit" encrypted with bcrypt
SOCKET_IO_ADMIN_PASSWORD_HASH=$2b$10$heqvAkYMez.Va6Et2uXInOnkCT6/uQj1brkrbyG3LpopDklcq7ZOS
11 changes: 11 additions & 0 deletions apps/ws/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join } from 'path';
import { Module } from '@nestjs/common';
import { RavenInterceptor, RavenModule } from 'nest-raven';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ServeStaticModule } from '@nestjs/serve-static';
import {
createNestLoggingModuleOptions,
LoggerModule,
Expand Down Expand Up @@ -40,6 +42,15 @@ if (process.env.SENTRY_DSN) {
useValue: new RavenInterceptor(),
});
}
if (!!process.env.SOCKET_IO_ADMIN_USERNAME && !!process.env.SOCKET_IO_ADMIN_PASSWORD_HASH) {
modules.push(
ServeStaticModule.forRoot({
rootPath: join(__dirname, '../node_modules/@socket.io/admin-ui/ui/dist'),
serveRoot: '/admin',
exclude: ['/api/(.*)'],
})
);
}

@Module({
imports: modules,
Expand Down
18 changes: 17 additions & 1 deletion apps/ws/src/socket/ws.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Server, Socket } from 'socket.io';
import { JwtService } from '@nestjs/jwt';
import { OnGatewayConnection, OnGatewayDisconnect, WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Logger } from '@nestjs/common';
import { instrument } from '@socket.io/admin-ui';

import { ISubscriberJwt, ObservabilityBackgroundTransactionEnum } from '@novu/shared';
import { IDestroy } from '@novu/application-generic';
Expand All @@ -18,7 +19,7 @@ export class WSGateway implements OnGatewayConnection, OnGatewayDisconnect, IDes
constructor(private jwtService: JwtService, private subscriberOnlineService: SubscriberOnlineService) {}

@WebSocketServer()
server: Server | null;
server: Server;

async handleDisconnect(connection: Socket) {
Logger.log(`New disconnect received from ${connection.id}`, LOG_CONTEXT);
Expand Down Expand Up @@ -199,4 +200,19 @@ export class WSGateway implements OnGatewayConnection, OnGatewayDisconnect, IDes
this.isShutdown = true;
await this.gracefulShutdown();
}

afterInit() {
if (!!process.env.SOCKET_IO_ADMIN_USERNAME && !!process.env.SOCKET_IO_ADMIN_PASSWORD_HASH) {
// For more information on how to use the admin UI, see https://socket.io/docs/v4/admin-ui/
instrument(this.server, {
auth: {
type: 'basic',
username: process.env.SOCKET_IO_ADMIN_USERNAME,
password: process.env.SOCKET_IO_ADMIN_PASSWORD_HASH,
},
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
namespaceName: '/admin',
});
}
}
}
58 changes: 54 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d55b1f2

Please sign in to comment.