Norvell, Please Merge


    1.0.0-alpha.1 • Public • Published

    React SignalR helper components

    npm version

    This package exposes a set of utility components to work with SignalR hub connections and streams. Storybook demo


    npm install @hwdtech/react-signalr


    Managing connections ins't easy, you have always to keep in mind when they should be closed, to prevent memory leaks and unnecessary resource allocations. React components on the other hand provides multiple ways to handle resources initialization and disposal: lifecycle hooks: componentDidMount and componentWillUnmount or new hooks API. These can be used to open and close underlying connections, so they can be used as disposable wrapper.

    The same is true for Hub streams, since they are observables, so in order to get any value from them one have to subscribe. But subscriptions also have to be disposed.

    Versions 1.x

    Coming soon, meanwhile you can hack into the examples:

    Versions 0.x

    SignalR hub connections

    This library utilizes component lifecycle hooks to manage connections and provides a declarative way to open/close hub connections and uses React context API

    import { HubConnectionBuilder } from "@aspnet/signalr";
    import { createConnectionContext } from "@hwdtech/react-signalr";
    const createHubConnection = () => {
      /* build a connection instance here, for example */
      return new HubConnectionBuilder().withUrl("/chathub").build();
    const { Provider, Consumer } = createConnectionContext(createHubConnection);
    /* later in the code */
        {/* somewhere deep in the application tree */}
          {({ connection, connectionError }) => {
            if (connection) return "Connected!";
            if (connectionError) return "Connection failed!";
            return "Connecting...";

    When Provider component is mounted it attempts to open a connection the createHubConnection factory return. When Provider component is unmounted it attempt to close the connection it opened previously.

    SignalR streams

    This library uses the same approach to dispose subscription as for closing hub connections.

    const { Provider, Consumer, createStreamSubscriber } = createConnectionContext(createHubConnection);
     * createStreamSubscriber will return a react component which props will be mapped to a stream arguments.
     * In other words, under the hood the following code will be invoked to setup a stream:
     * <code>
     *'ObservableCounter', props.count, props.interval);
     * </code>
    const CounterSubscriber = createStreamSubscriber(
      'ObservableCounter', // hub stream name
      props => [props.count, props.interval] // mapper function from component props to hub stream arguments
    /* later in the code */
    	{/* somewhere deep in the application tree */}
        <CounterSubscriber count={10} interval={1}>
        {({ value, error, done }) => {
    	  if (done) return 'Done!';
          if (error) return error.message;
          if (value != null) return value;
          return Connecting...

    When CounterSubscriber component is mounted it attempts to subscribe to a hub stream. The subscription will pass observable value to a children function. When CounterSubscriber is unmounted it attempts to dispose active subscription. Also note that CounterSubscriber don't need Consumer wrapper, since it uses it under the hood.

    Since the CounterSubscriber takes children function as property you can create your own component that will handle hub stream side effects, like in the example


    npm i @hwdtech/react-signalr

    DownloadsWeekly Downloads






    Unpacked Size

    12.7 kB

    Total Files


    Last publish


    • hwdtech-deploy
    • jacob-l
    • maxmalov
    • abukurov
    • 3