3.0.1 • Published 11 months ago
react-stomp-hooks v3.0.1
react-stomp-hooks
A react library to manage a application wide STOMP connection via SockJS or Websockets.
Install
npm install --save react-stomp-hooks
Usage
See also the supplied example project, and its sourcecode.
import React from "react";
import { StompSessionProvider, useSubscription } from "react-stomp-hooks";
const App = () => {
return (
//Initialize Stomp connection, will use SockJS for http(s) and WebSocket for ws(s)
//The Connection can be used by all child components via the hooks or hocs.
<StompSessionProvider
url={"https://stream.elite12.de/api/sock"}
//All options supported by @stomp/stompjs can be used here
>
<SubscribingComponent />
<SendingMessages />
</StompSessionProvider>
);
};
function SubscribingComponent() {
const [lastMessage, setLastMessage] = useState("No message received yet");
//Subscribe to /topic/test, and use handler for all received messages
//Note that all subscriptions made through the library are automatically removed when their owning component gets unmounted.
//If the STOMP connection itself is lost they are however restored on reconnect.
//You can also supply an array as the first parameter, which will subscribe to all destinations in the array
useSubscription("/topic/test", (message) => setLastMessage(message.body));
return <div>Last Message: {lastMessage}</div>;
}
export function SendingMessages() {
//Get Instance of StompClient
//This is the StompCLient from @stomp/stompjs
//Note: This will be undefined if the client is currently not connected
const stompClient = useStompClient();
const sendMessage = () => {
if (stompClient) {
//Send Message
stompClient.publish({
destination: "/app/echo",
body: "Echo 123",
});
} else {
//Handle error
}
};
return <Button onClick={sendMessage}>Send Message</Button>;
}
License
MIT © Sven Kirschbaum
3.0.0-alpha.1
12 months ago
3.0.0-alpha.3
12 months ago
3.0.0-alpha.2
12 months ago
3.0.0-alpha.5
11 months ago
3.0.0-alpha.4
12 months ago
3.0.1
11 months ago
3.0.0
11 months ago
2.2.1
1 year ago
2.2.0
1 year ago
2.1.0
3 years ago
2.1.0-beta.1
3 years ago
2.1.0-beta.0
3 years ago
2.0.3
3 years ago
2.0.4
3 years ago
2.0.2
4 years ago
2.0.1
4 years ago
2.0.0
4 years ago
1.2.4
4 years ago
1.2.3
4 years ago
1.2.2
4 years ago
1.2.1
4 years ago
1.2.0
4 years ago
1.1.0
4 years ago
1.0.3
4 years ago
1.0.0-test
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago
0.0.1
4 years ago