1.2.0 • Published 4 years ago

react-stomp-client v1.2.0

Weekly downloads
494
License
MIT
Repository
github
Last release
4 years ago

ReactStompClient

A simple STOMP message client for React. It will connect to the provided endpoint on mount, followed immmediately by a subscription to the provided topic. The connection gets cleaned up on unmount.

Easily testable via mock-stomp-broker!

Supports TypeScript.

Installing

npm install --save react-stomp-client

Usage example

import React, { Component } from "react";
import StompClient from "react-stomp-client";

class MyComponent extends Component {
  constructor(props) {
    this.state = {
      latestMessage: null
    };

    this.handleMessage = this.handleMessage.bind(this);
  }

  handleMessage(stompMessage) {
    this.setState({
      latestMessage: stompMessage
    });
  }

  render() {
    const { latestMessage } = this.state;
    return (
      <StompClient
        endpoint="ws://localhost:8888/websocket"
        topic="my-topic"
        onMessage={this.handleMessage}
      >
        <div>
          {latestMessage
            ? `Latest message received: ${latestMessage}`
            : "No message received yet"}
        </div>
      </StompClient>
    );
  }
}

Props

PropRequired?Description
endpointYesThe STOMP endpoint to connect to. The server should be up and ready to speak STOMP via WebSocket (the protocol should be either ws or wss).
topicNoThe STOMP topic to subscribe to. When no topic is provided, no subscription attempt is made.
onMessageNoThe callback to invoke when a STOMP message arrives.
childrenNoAny React component subtree. Use this to tie the together the lifecycles of the StompClient and any components that depend on data from it.
reconnectDelayNoThe delay in ms to wait before attempting to reconnect to the endpoint after a connections is interrupted. Defaults to 3000.
heartbeatIncomingNoThe heartbeat frequency in ms to request from the server. Defaults to 30000.
heartbeatOutgoingNoThe heartbeat frequency in ms to send to the server. Defaults to 30000.
1.2.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago