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

ably-react

v1.1.0

Published

**UNOFFICIAL** Opinionated React library for using Ably channels and clients. This library can handle multiple clients in a single page/app.

Downloads

215

Readme

ably-react

UNOFFICIAL
Opinionated React library for using Ably channels and clients. This library can handle multiple clients in a single page/app.

Getting started

Available hooks

useAbly

This hook registers and uses a realtime Ably client. You can pass a name to the useAbly hook to register multiple clients.

This hook can be used any number of times. Each hook will register a lock on the client. If all locks disappear the client will be destroyed and disconnected.

import { useAbly } from 'ably-react';

function App() {
  const ably = useAbly({
    // OPTIONAL: whether to skip client initialization for now.
    // useful when waiting for authentication
    skip: false,
    // OPTIONAL: can be used to identify the client
    clientId: 'user-id',
    // OPTIONAL [default: true]: whether to automatically connect the client
    autoConnect: true,
    // OPTIONAL [default: "default"]: the name to register the client under
    // the library allows for multiple clients (named)
    name: 'default',
    // OPTIONAL: Callback used to authenticate. This callback is a promise as opposed.
    authCallback: async (data) => {
      return fetchToken();
    },
    // OPTIONAL: Any other options to pass through to the client
    clientOptions: {},
  });
}

useAblyClient

This hook simply gets a client from the context by name (default by default). It does not register a lock on the client and simply returns the raw Ably.Realtime client.

const ablyClient = useAblyClient('default')

useAblyClientStatus

This hook simply gets a the client status from the context by name (default by default) As with the useAblyClient hook, this also does not register a lock on the client and simply returns the client status.

const ablyClientStatus = useAblyClientStatus('default')

useChannel

This hook simply gets a channel reference.

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')

useChannelStatus

This hook will track and return the status of a channel.

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')
const status = useChannelStatus(channel)

useIsClientPresent

This hook will keep track of whether a specific client is present. This is useful for peer to peer like communication.

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')
const isClientPresent = useIsClientPresent(channel, 'client-id', (error) => {
  // do something on error
});

usePresent

This hook will announce it's presence on a channel (and remove it if not rendered).

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')
usePresent(channel)

useRetryAttachUntil

This hook will retry attaching a channel until the callback returns false. This is useful for recovering failed channel states.

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')
useRetryAttachUntil(channel, 1000, (retryCount, event) => retryCount > 3, (event) => {
  // do something on failure
});

useHandshakeHandler

This hook should be used with the waitForHandshake utility (see below). This hook simply handles responding to a handshake request. (should only be rendered once per channel).

const ably = useAbly()
const channel = useChannel(ably.client, 'my-channel')
useHandshakeHandler(channel);

Utilities

waitForAttached

This utility returns a promise which waits for a channel to be attached (or rejects on timeout if specified).

await waitForAttached(channel, 1000); // if timeout is omitted, the promise will never reject

waitForMessage

This utility returns a promise which waits for a specific message from a channel (or rejects on timeout if specified).]

await waitForMessage(channel, (message) => message.name === 'ping', 1000); // if timeout is omitted, the promise will never reject

waitForHandshake

This utility returns a promise which will send a handshake request (and retry per interval), until the handshake is accepted by another client.

await waitForHandshake(
  channel,
  1000 /* retry interval */,
  1000 /* timeout */,
  (message) => true, /* only accept handshake if ... */
); // if timeout is omitted, the promise will never reject