Skip to content

Commit

Permalink
fix(vc-api): add heartbeat to sse
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloAyranji committed Jan 6, 2025
1 parent 3c0687d commit ea90dc0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
45 changes: 36 additions & 9 deletions apps/vc-api/src/api/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Body, Controller, Get, Inject, Param, Post, Query, Req, Res, Session, UseGuards} from '@nestjs/common';
import {Body, Controller, Get, Inject, Param, Post, Query, Req, Res, UseGuards, OnModuleInit, OnModuleDestroy} from '@nestjs/common';
import { filter, Subject, take } from 'rxjs';
import { v4 as uuidv4 } from 'uuid';
import { Response } from 'express';
import {
CREDENTIAL_CREATOR_FACADE,
ICredentialCreatorFacade
} from '../../core/applications/credentials/facade/icredential.facade';
import { Response } from 'express';
import { AUTH_CONTROLLER_MAPPER, IcredentialsControllerMapper } from './mapper/icredentials.controller.mapper';
import { v4 as uuidv4 } from 'uuid';
import { filter, Subject, take } from 'rxjs';
import { SubjectData } from './isubject.data';
import { JwtGuard } from '../../guards/jwt.guard';
import {CredentialsGenerateEmailOtpApiRequestQuery} from "./requests/credentials.generate-email-otp.request.api";
Expand All @@ -21,9 +21,10 @@ import { ChainId } from '../../core/domain/entities/environment';
type Siwens = { address: string, ens: string, chainId: ChainId };

@Controller('credentials')
export class CredentialsController {
export class CredentialsController implements OnModuleInit, OnModuleDestroy {

private authSubjects: Map<string, Subject<SubjectData>> = new Map();
private heartbeatInterval: NodeJS.Timer;

constructor(
@Inject(CREDENTIAL_CREATOR_FACADE)
Expand All @@ -33,6 +34,32 @@ export class CredentialsController {
private readonly authControllerMapper: IcredentialsControllerMapper
) {}

onModuleInit() {
this.heartbeatInterval = setInterval(() => {
this.sendHeartbeats();
}, 10000);
}

onModuleDestroy() {
if (this.heartbeatInterval) {
clearInterval(this.heartbeatInterval);
}
}

private sendHeartbeats() {
this.authSubjects.forEach((subject, authId) => {
try {
subject.next({
authId,
heartbeat: true,
});
} catch (error) {
console.error(`Failed to send heartbeat to ${authId}:`, error);
this.authSubjects.delete(authId);
}
});
}

@UseGuards(JwtGuard)
@Get('socials/:authName')
async getAuthUrl(
Expand All @@ -51,7 +78,6 @@ export class CredentialsController {
authId
)


res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Expand All @@ -61,11 +87,11 @@ export class CredentialsController {
res.write(`data: ${JSON.stringify({ redirectUrl })}\n\n`);

subject.pipe(
filter(data => data.authId === authId),
filter(data => data.authId === authId && !data.heartbeat),
take(1)
).subscribe(
(data) => {
res.write(`data: ${JSON.stringify({ result:data.result })}\n\n`);
res.write(`data: ${JSON.stringify({ result: data.result })}\n\n`);
res.end();
this.authSubjects.delete(authId);
},
Expand All @@ -74,7 +100,7 @@ export class CredentialsController {
res.end();
this.authSubjects.delete(authId);
}
);
)
}

@Get('socials/:authName/callback')
Expand All @@ -95,6 +121,7 @@ export class CredentialsController {
const subject = this.authSubjects.get(authId);
subject?.next({
authId,
heartbeat: false,
result: {
verifiableCredential,
dataKey
Expand Down
3 changes: 2 additions & 1 deletion apps/vc-api/src/api/credentials/isubject.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { VerifiableEthereumEip712Signature2021 } from '../../core/domain/entitie

export interface SubjectData {
authId: string;
result: {
heartbeat: boolean
result?: {
verifiableCredential: VerifiableEthereumEip712Signature2021;
dataKey: string;
};
Expand Down

0 comments on commit ea90dc0

Please sign in to comment.