MessageBus
The MessageBus is used to send and receive messages in a decoupled way, both client-side and edge-side.
Usage
Client-side
Outsmartly provides a React Hook to gain access to the MessageBus from anywhere in your UI components:
import { useMessageBus } from '@outsmartly/message-bus';
export function Home() {
const messageBus = useMessageBus();
const [clickCount, setClickCount] = useState(0);
useEffect(() => {
// Passing data as a second argument is optional
messageBus.emit('YourCustomMessages.HOME_COMPONENT_MOUNTED');
}, []);
return (
<button
onClick={() => {
setClickCount((clickCount) => {
const newClickCount = clickCount + 1;
const data = {
count: newClickCount,
};
messageBus.emit('YourCustomMessages.BUTTON_CLICKED', data);
return newClickCount;
});
}}
>
You have clicked me {clickCount} times
</button>
);
}Edge-side
At the edge, you can gain access to the MessageBus from any OutsmartlyEvent passed to middleware, interceptors, or overrides:
When listening, it's more common to want to set up a global listener once, when the edge starts up. This can be done using the plugin interface:
Last updated
Was this helpful?