react-stomp-hooks
TypeScript icon, indicating that this package has built-in type declarations

2.2.1 • Public • Published

react-stomp-hooks

A react library to manage a application wide STOMP connection via SockJS or Websockets.

NPM Build JavaScript Style Guide

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 />
    </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>
  );
}

License

MIT © Sven Kirschbaum

Readme

Keywords

none

Package Sidebar

Install

npm i react-stomp-hooks

Weekly Downloads

4,898

Version

2.2.1

License

MIT

Unpacked Size

73.8 kB

Total Files

24

Last publish

Collaborators

  • fallobst22