skip to Main Content

I am tring to implement server-sent events functionality in a firebase function where it will continuously send back data to the client side while the function is executing rather than sending back data when the function completes. Everything Iv’e tried just makes the function return any data at the end of the execution. Is it even possible to stream data back to client side from a firebase function or does it cache and buffer everything until it finishes executing? Is there another way to execute this functionality with a firebase function that would be done differently?

I am tring to implement server-sent events functionality in a firebase function where it will continuously send back data to the client side while the function is executing rather than sending back data when the function completes. Everything Iv’e tried just makes the function return any data at the end of the execution.

here is my code so far:

functions/index.js:

const functions = require("firebase-functions");
const express = require("express");

const app = express();

app.get("/sse", (request, response) => {
  // Set headers to indicate that the response is an SSE stream
  response.setHeader("Content-Type", "text/event-stream");
  response.setHeader("Cache-Control", "no-cache");
  response.setHeader("Connection", "keep-alive");

  // Send initial message to client
  response.write("data: Connectednn");

  // Send data to the client every second
  const intervalId = setInterval(() => {
    const data = { message: "Hello world!" };
    response.write(`data: ${JSON.stringify(data)}nn`);
    response.flush(); // Send buffered data to client immediately
  }, 1000);

  // End the response when the client closes the connection
  request.on("close", () => {
    clearInterval(intervalId);
    response.end();
  });
});

exports.sseFunction = functions.https.onRequest(app);

and then my function in the client side:

export const sseFunctionStream = async () => {
  const eventSource = new EventSource(
    "https://us-central1-<project-id>.cloudfunctions.net/sseFunction/sse"
  );

  eventSource.addEventListener("message", (event) => {
    const data = JSON.parse(event.data);
    console.log(data);
  });

  return () => {
    eventSource.close();
  };
};

ive tried severl implementations, but they all result with the same outcome that the event listener does not get any data while the function is executing until it finishes. Once it finishes it gets all of the data. I want it to console log "Hello World" every second on the client side as it listens for events, just as it does in the function.

2

Answers


  1. Seems that it’s not possible to make Firebase Functions support SSE/event-stream due to buffering mechanism. Ref: https://stackoverflow.com/a/48431968/802848

    You could consider using Cloud Run, GAE or writing to Firebase DB as an alternative.

    Login or Signup to reply.
  2. Possible alternatives: Supabase supports server-sent events
    Docs

    Also, Vercel edge functions support streaming data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search