Skip to content

Instantly share code, notes, and snippets.

@oxPraGa
Created December 22, 2021 11:08
Show Gist options
  • Save oxPraGa/35314bbfa6447aa16a61c721d1f681de to your computer and use it in GitHub Desktop.
Save oxPraGa/35314bbfa6447aa16a61c721d1f681de to your computer and use it in GitHub Desktop.
Websocket nestjs health check indicator
import { Injectable } from '@nestjs/common'
import { HttpService } from '@nestjs/axios'
import {
HealthIndicator,
HealthIndicatorResult,
HealthCheckError
} from '@nestjs/terminus'
import { firstValueFrom } from 'rxjs'
@Injectable()
export class WebSocketHealthIndicator extends HealthIndicator {
constructor(private httpService: HttpService) {
super()
}
async checkHealth(key: any, options: any): Promise<HealthIndicatorResult> {
let isHealthy = false
try {
const result: any = await firstValueFrom(
this.httpService.get(
`http://127.0.0.1:${process.env.SOCKET_PORT}/socket.io/?EIO=4&transport=polling`
)
)
if (result.status == 200) {
isHealthy = true
}
} catch {}
if (isHealthy) return this.getStatus(key, isHealthy)
if (options.required) {
throw new HealthCheckError(
'WebSocket Server',
this.getStatus(key, isHealthy, { message: 'Web Server not working' })
)
}
return this.getStatus(key, isHealthy)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment