Skip to content

Commit

Permalink
378th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 23, 2024
1 parent 80879f3 commit 5cea98c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
26 changes: 0 additions & 26 deletions app/src/routes/sse/event/+handler.ts

This file was deleted.

45 changes: 45 additions & 0 deletions app/src/routes/sse/event/[id]/+handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { EventEmitter } from 'node:events';
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';

export default (async (app) => {
const sseEmitter = new EventEmitter();

app.get(
'',
{
schema: {
params: Type.Object({
id: Type.String(),
}),
},
},
(req, reply) => {
const { id } = req.params;

sseEmitter.on(`/sse/event/${id}`, (data) => {
reply.sse({ id, data });
});

req.raw.on('close', () => {
reply.sse({ event: 'close' });
});
},
);

app.post(
'',
{
schema: {
params: Type.Object({
id: Type.String(),
}),
},
},
async (req, reply) => {
const { id } = req.params;
sseEmitter.emit(`/sse/event/${id}`, req.body);
return reply.send({ message: 'Data sent to client' });
},
);
}) as FastifyPluginAsyncTypebox;

0 comments on commit 5cea98c

Please sign in to comment.