Better SSE can be used with any web-server framework that uses the underlying Node HTTP module . This section shows example usage with some popular HTTP frameworks.
Feel free to submit a PR with a minimal example for another framework!
import { createServer } from " http " ;
import { createSession } from " better-sse " ;
const server = createServer ( async ( req , res ) => {
const session = await createSession (req , res) ;
session . push ( " Hello world! " ) ;
res . writeHead ( 404 ) . end () ;
import express from " express " ;
import { createSession } from " better-sse " ;
app . get ( " /sse " , async ( req , res ) => {
const session = await createSession (req , res);
session . push ( " Hello world! " );
import Router from " @koa/router " ;
import { createSession } from " better-sse " ;
const router = new Router ();
router . get ( " /sse " , async ( ctx ) => {
// Prevent Koa sending a response and closing the connection
const session = await createSession (ctx . req , ctx . res );
session . push ( " Hello world! " );
app . use (router . routes ());
Assuming you are using @nestjs/platform-express
(the default).
import { Controller, Get, Req, Res } from " @nestjs/common " ;
import { Request, Response } from " express " ;
import { createSession } from " better-sse " ;
export class SseController {
async sse ( @Req () req : Request , @Res () res : Response ) {
const sse = await createSession (req , res);
sse . push ( " Hello world! " );
Currently only API routes under the Pages Router is supported.
import { NextApiRequest, NextApiResponse } from " next " ;
import { createSession } from " better-sse " ;
export default async function handler (
const session = await createSession (req , res);
session . push ( " Hello, world! " );
Issue #79 tracks support for the Fetch API Request and Response objects which will enable compatibility with Route Handlers under the App Router .
import Fastify from " fastify " ;
import { createSession } from " better-sse " ;
const fastify = Fastify ();
fastify . get ( " /sse " , async ( request , reply ) => {
const session = await createSession (request . raw , reply . raw );
session . push ( " Hello world! " );
fastify . listen ({ port: 8080 });
import router from " @adonisjs/core/services/router " ;
import { createSession } from " better-sse " ;
router . get ( " /sse " , async ( { request , response } ) => {
const session = await createSession (request . request , response . response );
session . push ( " Hello world! " );
import Hapi from " @hapi/hapi " ;
import { createSession } from " better-sse " ;
const init = async () => {
const server = Hapi . server ( {
handler : async ( { raw }, { abandon } ) => {
const session = await createSession (raw . req , raw . res ) ;
session . push ( " Hello world! " ) ;