React hooks based Mercure client for subscription to topics.
Supports only token based authentication, cookie based auth currently not implemented.
-
yarn add @bornfight/mercureact
ornpm i @bornfight/mercureact
-
install peer dependency (eventsource)
yarn add eventsource
ornpm i eventsource
useMemo
is recommended (or some other memoization method) for preventing re-instantiation of useSubscribe
hook.
import { SubscribeConfig, useSubscribe } from "@bornfight/mercureact";
const config: SubscribeConfig = useMemo(() => {
const fooTopic = `/foo/1`;
return {
url: "http://localhost:5000/.well-known/mercure",
token: "my-token",
topics: [fooTopicc],
onMessage: (message) => {
// messages are not de-serialized internally
const serializedMessage = message.data
? JSON.parse(message.data)
: undefined;
console.log(serializedMessage)
},
};
}, []);
const { eventSource } = useSubscribe(config);