This library provides a hook for simple use of stompjs in React npm
❗️Major Updated! The Hook has to be used with Provider. Please check the example below.
const {disconnect, subscribe, unsubscribe, subscriptions, send, isConnected} = useStomp();
This hook automatically manages the entire React App to be a single websocket connection. So feel free to use hook any components.
- disconnect : Disconnect webscoket from server.
- subscribe : Subscribe sepcific destination
- unsubscribe : Unsubscribe sepcific destination
- subscriptions : Returns all destinations you are currently subscribed to.
- send : Send message with body and headers to specific destination
- isConnected : Returns the current connection status.
// your Root Component
<StompProvider config={{ brokerURL: SERVER_STOMP_URL }}>
<App />
</StompProvider>
subscribe("/room/...", (body) => {
// Body is Object Changed to JSON
});
send("/room/...", message, headers);
useEffect(() => {
subscribe("/room/...", (body) => {
// do anything...
});
return () => {
// Make sure you're subscribed
if (subscriptions["/room/..."]) {
unsubscribe("/room/...");
}
};
}, []);
console.log(isConnected); // true or false