Skip to content

Commit

Permalink
Merge pull request #1780 from Giveth/feat/stellar_integration
Browse files Browse the repository at this point in the history
fix: change from WS to SSE
  • Loading branch information
Meriem-BM authored Aug 25, 2024
2 parents 0210f4c + a14ccba commit ddda14d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 64 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@
"twitter-api-sdk": "^1.0.9",
"type-graphql": "2.0.0-beta.1",
"typedi": "0.8.0",
"typeorm": "0.3.20",
"ws": "^8.18.0"
"typeorm": "0.3.20"
},
"lint-staged": {
"*.ts": [
Expand Down
10 changes: 7 additions & 3 deletions src/resolvers/draftDonationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { RecurringDonation } from '../entities/recurringDonation';
import { checkTransactions } from '../services/cronJobs/checkQRTransactionJob';
import { findProjectById } from '../repositories/projectRepository';
import { notifyDonationFailed } from '../services/sse/sse';

const draftDonationEnabled = process.env.ENABLE_DRAFT_DONATION === 'true';
const draftRecurringDonationEnabled =
Expand Down Expand Up @@ -419,9 +420,12 @@ export class DraftDonationResolver {
);

// Notify clients of new donation
(global as any).notifyDraftDonationFailed({
draftDonationId: id,
expiresAt: draftDonation.expiresAt,
notifyDonationFailed({
type: 'draft-donation-failed',
data: {
draftDonationId: id,
expiresAt: draftDonation.expiresAt,
},
});

return true;
Expand Down
15 changes: 7 additions & 8 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginSchemaReporting } from '@apollo/server/plugin/schemaReporting';
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
import express, { json, Request } from 'express';
import express, { json, Request, Response } from 'express';
import { Container } from 'typedi';
import { Resource } from '@adminjs/typeorm';
import { validate } from 'class-validator';
Expand Down Expand Up @@ -68,7 +68,7 @@ import { runDraftDonationMatchWorkerJob } from '../services/cronJobs/draftDonati
import { runCheckUserSuperTokenBalancesJob } from '../services/cronJobs/checkUserSuperTokenBalancesJob';
import { runCheckPendingRecurringDonationsCronJob } from '../services/cronJobs/syncRecurringDonationsWithNetwork';
import { runCheckQRTransactionJob } from '../services/cronJobs/checkQRTransactionJob';
import { startWebSocketServer } from '../services/ws/webSocketServer';
import { addClient } from '../services/sse/sse';

Resource.validate = validate;

Expand Down Expand Up @@ -290,13 +290,12 @@ export async function bootstrap() {
app.post('/fiat_webhook', onramperWebhookHandler);
app.post('/transak_webhook', webhookHandler);

const httpServer = http.createServer(app);
// Route to handle SSE connections
app.get('/events', (_req: Request, res: Response) => {
addClient(res);
});

// Start WebSocket server
const { notifyDonationAdded, notifyDraftDonationFailed } =
startWebSocketServer(httpServer);
(global as any).notifyDonationAdded = notifyDonationAdded;
(global as any).notifyDraftDonationFailed = notifyDraftDonationFailed;
const httpServer = http.createServer(app);

await new Promise<void>((resolve, reject) => {
httpServer
Expand Down
10 changes: 7 additions & 3 deletions src/services/cronJobs/checkQRTransactionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { findUserById } from '../../repositories/userRepository';
import { relatedActiveQfRoundForProject } from '../qfRoundService';
import { QfRound } from '../../entities/qfRound';
import { syncDonationStatusWithBlockchainNetwork } from '../donationService';
import { notifyClients } from '../sse/sse';

const STELLAR_HORIZON_API =
(config.get('STELLAR_HORIZON_API_URL') as string) ||
Expand Down Expand Up @@ -182,9 +183,12 @@ export async function checkTransactions(
});

// Notify clients of new donation
(global as any).notifyDonationAdded({
donationId: returnedDonation.id,
draftDonationId: donation.id,
notifyClients({
type: 'new-donation',
data: {
donationId: returnedDonation.id,
draftDonationId: donation.id,
},
});

return;
Expand Down
45 changes: 45 additions & 0 deletions src/services/sse/sse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Response } from 'express';

let clients: Response[] = [];
type TNewDonation = {
type: 'new-donation';
data: {
donationId: number;
draftDonationId: number;
};
};

type TDraftDonationFailed = {
type: 'draft-donation-failed';
data: {
draftDonationId: number;
expiresAt?: Date;
};
};

// Add a new client to the SSE stream
export function addClient(res: Response) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');

res.flushHeaders();

clients.push(res);

// Remove the client on disconnect
res.on('close', () => {
clients = clients.filter(client => client !== res);
});
}

// Notify all connected clients about a new donation
export function notifyClients(data: TNewDonation) {
clients.forEach(client => client.write(`data: ${JSON.stringify(data)}\n\n`));
}

// Notify all connected clients about a failed donation
export function notifyDonationFailed(data: TDraftDonationFailed) {
clients.forEach(client => client.write(`data: ${JSON.stringify(data)}\n\n`));
}
46 changes: 0 additions & 46 deletions src/services/ws/webSocketServer.ts

This file was deleted.

0 comments on commit ddda14d

Please sign in to comment.