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

@upsub/client

v0.2.0

Published

JavaScript client for UpSub. A High performance Pub/Sub messaging server for the Web & Cloud.

Downloads

6

Readme

JavaScript client

npm version Build Status gzip size

This is the JavaScript client for UpSub, its supports node, browser and web worker environments.

Install

npm install --save @upsub/client

Usage

import Client from '@upsub/client'

const client = new Client('ws://localhost:4400', {
  // options
})

Connects to the dispatcher server.

Options

  • name: String: Name connection, useful for debugging.
  • appID: String: App identifier for the dispatcher authentication.
  • secret: String: Secret key for authentication.
  • public: String: Public key for authentication.
  • reconnectionDelay: Number: Delay between client reconnection attemps, default 2000 ms.
  • pingInterval: Number: The client will fire a ping message to the dispatcher each interval, default 30000 ms (30s).
  • pongTimeout: Number: The client should receive a pong message within the timeout, the client will try to reconnect otherwise.
  • subscriptionTimeout: Number: If the dispatcher doesn't send a subscription acknowledgement back within the timeout 2000 ms default, then the client will notify the server again.
  • requestTimeout: Number: Request should receive a response within the given time otherwise reject, default is 5000 ms.

Subscribe to channels

The best way to structure event streams is by scoping events to a specific or multiple channels.

// Subscribe to a single channel
const channel = client.channel('some-channel')

// Subscribe to multiple channels
const multipleChannels = client.channel('channel-1', 'channel-2')

The channel method returns a channel object which events listeners can be bound to.

Unsubscribe from channels

To unsubscribe from a channel, invoke the unsubscribe method on the channel object.

// Removes all channels subscriptions
channel.unsubscribe()

// Remove a specific channel subscription
channel.unsubscribe('channel-1')

// Remove multiple specific channel subscription
channel.unsubscribe('channel-1', 'channel-2')

When a channel is unsubscribed, all bound event listeners will also be removed.

Listen for events

The event system is based on the default EventEmitter in node and follows the same api with some additions.

// Bind event listener to a channel
channel.on('some-event', function (msg) {
  console.log(msg)
})

// Bind event listener on the client
client.on('some-event', msg => console.log(msg))

Remove event listener

To remove event listeners, invoke the off method on a channel or the client instance.

// Remove event listener from channel
channel.off('some-event')

// Remove event listener from client
client.off('some-event')

Send message

Send a message to the UpSub Dispatcher.

// Send message on channel
channel.send('some-event', { hello: 'world!' })

// Send message directly from the client instance
client.send('some-event', 'message')

Send request

Send a request message on a channel to another listening client, the receiving client should send a response back or the sender will throw an Error.

// Send request
client.request('channel', { key: 'value' })
  .then(res => console.log(res))
  .catch(err => console.log(err)) // Nobody sent a response back

// Send response back
client.on('channel', (payload, msg, reply) => {
  reply('Send some data back...')
})

Close connection

client.close()

License

Released under the MIT License