npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hwdtech/react-signalr

v1.0.0-alpha.1

Published

A set of helper components to work with SignalR hub connections & streams

Downloads

27

Readme

React SignalR helper components

npm version

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

Installation

npm install @hwdtech/react-signalr

Motivation

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:

  • How to use connection context hook: https://github.com/hwdtech/react-signalr/blob/master/examples/ConnectionStatus.stories.tsx
  • How to use connection stream hook: https://github.com/hwdtech/react-signalr/blob/master/examples/ConnectionStream.stories.tsx

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 */

<Provider>
  <div>
    {/* somewhere deep in the application tree */}
    <Consumer>
      {({ connection, connectionError }) => {
        if (connection) return "Connected!";
        if (connectionError) return "Connection failed!";
        return "Connecting...";
      }}
    </Consumer>
  </div>
</Provider>;

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>
 *   connection.stream('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 */
<Provider>
  <div>
	{/* 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...
    }}
    </CounterSubscriber>
  </div>
</Provider>;

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