Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a server that emits data at regular intervals. I want to use this data in my rest API, how do I fetch it? It needs to be automatically called when the data is pushed from the external source. I have tried the following code but it did not work.

var EventSource = require("eventsource");
var url = "..." // Source URL
    var es =  new EventSource(url);
    es.onmessage = (event) => {
        console.log(event)
        const parsedData = JSON.parse(event.data);
        console.log(parsedData)
                Why do you want to use the data in REST API? REST API and SSE are different approaches. REST API is initiated by the client side and client waits for the response, and its different from the SSE.
– IRSHAD
                Oct 4, 2020 at 19:09
                I have an external source which supplies me data regularly, I need to extract the data, make some modifications and store it into a database. That’s the reason I want to use it in my backend rest server.
– Kowshhal
                Oct 5, 2020 at 18:05
                When that external source push the data, you have to listen for it by subscribing to its events and save it to the database (or you could also consider writing a webhook for the same). Thus, whenever you call your REST API next time, you will be able to fetch the latest data from the database.
– IRSHAD
                Oct 6, 2020 at 0:46
                Can you provide a code sample ? I tried it but it’s doesn’t work. Check the code which I tried above.
– Kowshhal
                Oct 6, 2020 at 4:23

I had the same problem when I wanted to consume the SSE from a different microservice , I followed this approach and it worked for me.

node.js file

const  eventSource = require('eventsource');
  async socEventStream(req, res) {
    // list of the event you want to consume
    const list = ['EVENT1_NAME', 'EVENT2_NAME','EVENT3_NAME'];
    try {
      const e = new eventSource('url//of_sse_event', {});
      for (const l of list) {
        e.addEventListener(l, (e) => {
          const data = e.data;
         // Your data
          console.log('event data =====>',data)
      res.on('close', () => {
        for (const l of list) {
          e.removeEventListener(l, (e) => {
    } catch (err) {
      console.log(err)

if you want to consume event on node.js and send it to client then

const eventSource = require('eventsource'); async socEventStream(req, res) { // setting express timeout for more 24 hrs req.setTimeout(24 * 60 * 60 * 1000); // setting headers for client to send consumed sse to client const headers = { 'Content-Type': 'text/event-stream', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,observe,x-access-key', 'Access-Control-Allow-Methods': 'POST, PUT, GET, OPTIONS, DELETE', 'Access-Control-Allow-Origin': '*', res.setTimeout(24 * 60 * 60 * 1000); res.writeHead(200, headers); // list of the event you want to consume const list = ['EVENT1_NAME', 'EVENT2_NAME','EVENT3_NAME']; try { const e = new eventSource('url//of_sse_event', {}); for (const l of list) { e.addEventListener(l, (e) => { const data = e.data; // Your data res.write(`event:${l}\ndata:${data}\n\n`); req.on('close', () => { for (const l of list) { e.removeEventListener(l, (e) => { } catch (err) { console.log(err)

For testing purposes set up something like this on the server. Create a stream with and event name so you can listen for it on the client.

const SseStream = require('ssestream')
app.get('/sse', (req, res) => {
  console.log('new connection')
  const sseStream = new SseStream(req)
  sseStream.pipe(res)
  const pusher = setInterval(() => {
    sseStream.write({
      event: 'server-time',
      data: new Date().toTimeString()
  }, 1000)
  res.on('close', () => {
    console.log('lost connection')
    clearInterval(pusher)
    sseStream.unpipe(res)

And on the client you listen for the event like this

var EventSource = require('eventsource')
var es = new EventSource(url)
es.addEventListener('message', function (e) {
  console.log(e.data)
                I want to consume sse on my express server, Not on the client-side. I already have an internal source that provides me with data periodically. I just need to consume it but I can't do it with the code I provided.
– Kowshhal
                Oct 5, 2020 at 3:28
                it's with an event listener with the name of the event from the server. not the same as your code.
– C.Gochev
                Oct 6, 2020 at 16:34
                What if I don’t know the name of the event? Is it not possible to listen on the sse server for any event? I am able to do it in other languages but not finding anything in nodejs.
– Kowshhal
                Oct 6, 2020 at 20:03
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.