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

fix: HttpService Injection in WebsocketService for Push Notification Sending #37

Merged
merged 2 commits into from
Nov 13, 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
2 changes: 1 addition & 1 deletion packages/server/src/websocket/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
result: 'error',
id: 1,
})
this.logger.error(`WebSocket server listening on port ${WS_PORT}`)
this.logger.log(`WebSocket server listening on port ${WS_PORT}`)
} catch (error) {
this.logger.error('Error during WebSocket server initialization', error.stack)
throw error
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/websocket/websocket.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { MongooseModule } from '@nestjs/mongoose'
import { StoreQueuedMessageSchema, StoreQueuedMessage } from './schemas/StoreQueuedMessage'
import { StoreLiveSessionSchema, StoreLiveSession } from './schemas/StoreLiveSession'
import { MessagePersister } from './services/MessagePersister'
import { HttpModule } from '@nestjs/axios'

@Module({
imports: [
MongooseModule.forFeature([
{ name: StoreQueuedMessage.name, schema: StoreQueuedMessageSchema },
{ name: StoreLiveSession.name, schema: StoreLiveSessionSchema },
]),
HttpModule,
],
providers: [WebsocketGateway, WebsocketService, MessagePersister],
})
Expand Down
15 changes: 14 additions & 1 deletion packages/server/src/websocket/websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { JsonRpcResponseSubscriber } from './interfaces/interfaces'
@Injectable()
export class WebsocketService {
private readonly logger: Logger
private readonly httpService: HttpService
private readonly redisSubscriber: Redis
private readonly redisPublisher: Redis
private server: Server
Expand All @@ -33,6 +32,7 @@ export class WebsocketService {
@InjectModel(StoreQueuedMessage.name) private queuedMessage: Model<StoreQueuedMessage>,
@InjectRedis() private readonly redis: Redis,
private configService: ConfigService,
private readonly httpService: HttpService,
) {
this.logger = new Logger(WebsocketService.name)
this.redisSubscriber = this.redis.duplicate()
Expand Down Expand Up @@ -452,6 +452,19 @@ export class WebsocketService {
// Retrieves the push notification URL from the configuration service
const pushNotificationUrl = this.configService.get<string>('appConfig.pushNotificationUrl')

if (!pushNotificationUrl) {
this.logger?.error('[sendPushNotification] Push notification URL is not defined in appConfig')
return false
}

this.logger?.debug(`[sendPushNotification] pushNotificationUrl: ${pushNotificationUrl}`)
if (!token || !messageId) {
this.logger?.error('[sendPushNotification] Invalid token or messageId')
return false
}

this.logger?.debug(`[sendPushNotification] token: ${token} --- messageId: ${messageId}`)

// Sends the push notification via HTTP POST request
const response = await lastValueFrom(
this.httpService.post(pushNotificationUrl, {
Expand Down
2 changes: 2 additions & 0 deletions packages/server/test/WebsocketTestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { WebsocketGateway } from '../src/websocket/websocket.gateway'
import { MongooseModule } from '@nestjs/mongoose'
import { StoreQueuedMessageSchema, StoreQueuedMessage } from '../src/websocket/schemas/StoreQueuedMessage'
import { StoreLiveSessionSchema, StoreLiveSession } from '../src/websocket/schemas/StoreLiveSession'
import { HttpModule } from '@nestjs/axios'

@Module({
imports: [
MongooseModule.forFeature([
{ name: StoreQueuedMessage.name, schema: StoreQueuedMessageSchema },
{ name: StoreLiveSession.name, schema: StoreLiveSessionSchema },
]),
HttpModule,
],
providers: [WebsocketGateway, WebsocketService],
})
Expand Down
Loading