@isimisi/adonis-sse
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

SSE Plugin for AdonisJS v5

This plugin is an updated version of adonis-sse from the following repository https://github.com/stitchng/adonis-sse

Getting Started

npm i @isimisi/adonis-sse
yarn add @isimisi/adonis-sse

Configure

node ace configure @isimisi/adonis-sse

Register

Register the following middleware inside start/kernel.js

Server.middleware.registeredNamed({
   sse: () => import ("@ioc:isimisi/SSE")
})

or optionally register it as a gloabl middleware

Usage

Route.get("/sse", ({ sse }) => {
    sse.send({ message: "To the moon!🚀" });
}).middleware(["sse"])

Connecting in React

import { useEffect, useState } from "react";

function MyComponent() {
    const [message, setMessage] = useState("");

    useEffect(() => {
        const eventSource = new EventSource("http://localhost:3333/sse");

        eventSource.addEventListener("message", (event) => {
            const data = JSON.parse(event.data); // { message: "To the moon!🚀" }
            setMessage(data.message);
        }, false)

        return () => eventSource.close();
    }, [])

    return(
        <div>{message}</div>
    )
}

export default MyComponent

Disclamer

I have not created tests via. japa but I have tested the functionallity. Please feel free to report any issues.

Readme

Keywords

none

Package Sidebar

Install

npm i @isimisi/adonis-sse

Weekly Downloads

49

Version

0.0.7

License

MIT

Unpacked Size

41.2 kB

Total Files

48

Last publish

Collaborators

  • isimisi