Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ws): Mount Socket.io Admin UI #5435

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,8 +1,9 @@
const nr = require('newrelic');
import { Server, Socket } from 'socket.io';
import { JwtService } from '@nestjs/jwt';
import { OnGatewayConnection, OnGatewayDisconnect, WebSocketGateway, WebSocketServer } from '@nestjs/websockets';

Check warning on line 4 in apps/ws/src/socket/ws.gateway.ts

View workflow job for this annotation

GitHub Actions / Spell check

Unknown word (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 @@
constructor(private jwtService: JwtService, private subscriberOnlineService: SubscriberOnlineService) {}

@WebSocketServer()
server: Server | null;
server: Server;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 praise: Nice tidy up 🧹‏


async handleDisconnect(connection: Socket) {
Logger.log(`New disconnect received from ${connection.id}`, LOG_CONTEXT);
Expand Down Expand Up @@ -199,4 +200,19 @@
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, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Could we add a URL link to the configuration options for Socket.io? I find it useful to have a URL to click on to easily find the other options in case of maintenance + debugging. Specifically, I would like to know what effect the mode has on the admin panel.

Copy link
Contributor Author

@SokratisVidros SokratisVidros Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean as a comment? If yes, done!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! 🚀

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.

Loading