Giter Site home page Giter Site logo

prakhargoel8c / stomp-hooks-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from svenkirschbaum/react-stomp-hooks

0.0 0.0 0.0 3.67 MB

This repository contain a react library which enables simple access to stomp subscriptions via hooks.

License: MIT License

JavaScript 40.45% TypeScript 51.00% CSS 1.47% HTML 7.08%

stomp-hooks-react's Introduction

react-stomp-hooks

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

NPM Travis 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 © fallobst22

stomp-hooks-react's People

Contributors

svenkirschbaum avatar dependabot[bot] avatar kirkegaard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.