@isimisi/adonis-sse
v0.0.8
Published
Adonis SSE plugin
Downloads
59
Readme
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.