Implementing server sent events in FeatherJS

Viewed 114

How do we implement server sent events in FeathersJS, normally for express based applications we have

app.get('/sse', (req, res, next) => {

   res.status(200).set({
     "Connection": "keep-alive",
     "cache-control" : "no-cache",
     "Content-Type" : "text/event-stream"
});

how to do the same in feathers JS any help would be appreciated

1 Answers

I got SSE running in a FeathersJS app - it works great, except that the app seems to hang while a SSE request is pending (listening on events):

import { createSession, createChannel } from 'better-sse';
import { IncomingMessage, ServerResponse } from 'http';

export default function (app: any): void {
  const debugSseSource = require('debug')('sseSource');
  const sseChannel = createChannel();

  // Order creations
  const orders: any = app.service('orders'); // use endpoint URL

  app.get('/v1/admin/orders', async (req: IncomingMessage, res: ServerResponse) => {
    // on new connections, add SSE session to SSE channel
    const sseSession = await createSession(req, res);
    sseChannel.register(sseSession);
    debugSseSource('Client (orders) has joined.');
    //res.end(); // @TODO/PROBLEM without the whole FeathersJS app hangs - but it stops the request...
  });

  orders.on('created', (order: any) => {
    // broadcast to all clients
    debugSseSource('order created');
    sseChannel.broadcast(order, 'created');
  });
};

Related FeathersJS GitHub issue: https://github.com/feathersjs/feathers/issues/369

Related