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

pushex

v1.0.0

Published

JS client for PushEx (https://github.com/pushex-project/pushex)

Downloads

1,007

Readme

Pushex.js

npm Hex.pm Build Status

PushEx is an Elixir based push-only websocket implementation. Pushex.js is the JS client to the server.

Links

Installation

You can install Pushex.js by using npm:

npm install --save pushex

Documentation ToC

Usage

Pushex is exposed via an exported class Pushex:

import { Pushex } from 'pushex'

This class can be used to create an instance of Pushex. Each instance of Pushex maintains its own state and does not share any state with other Pushex instances. Thus, it's possible to run multiple Pushex clients on the same webpage.

const pushex = new Pushex('wss://example.myserver.com/push_socket', {})
pushex.subscribe('user-1').bind('*', (event, data) => {
  console.log('user-1 channel received event/data', event, data)
})

Pushex constructor can accept several optional parameters that are important for extending the library for any use case:

const pushex = new Pushex('wss://example.myserver.com/push_socket', {
  getParams: () => Promise.resolve({ token: 'myToken' }),
  onConnect: (pushEx) => {},
  onConnectionError: (pushEx) => {},
  socketReconnectAlgorithm: (tries) => {
    return [3000, 6000, 10000, 20000][tries - 1] || 30000
  }
})

Authentication

All authentication / authorization occurs on your hosted server running PushEx during the lifecycle points socket_connect and channel_join.

Methods

All public methods are detailed below.

Pushex#constructor

constructor(url, { getParams, onConnect, onConnectionError, socketReconnectAlgorithm })
  • getParams - Function that returns a promise resolving parameters passed to the server on connection. This is only ever invoked in connect or explicit using resetParams.
  • onConnect - This is invoked when the socket is opened
  • onConnectionError - This is invoked when the socket encounters an error
  • socketReconnectAlgorithm - This is invoked when the socket is determining how it should back off. The return value is in ms. The default implementation is [3000, 6000, 10000, 20000][tries - 1] || 30000 and does not need specified

Pushex#connect

connect()

Resolves connection params and connects to the push socket. If the socket is already connected, the socket will not be disconnected.

Pushex#disconnect

disconnect()

Disconnects the push socket.

Pushex#resetParams

resetParams()

Fetches params using the provided getParams function and sets the params on the socket. This will not change the connected socket, but will affect future connection attempts, including reconnection attempts.

Pushex#subscribe

subscribe(channelName)

This is the main interface to accessing push data. A Subscription will be created and memoized for future usage. The channelName is used to connect as a Phoenix channel. In PushEx, any channel name you want to use is valid, and the authorization layer can be used to deny invalid channels.

Pushex#unsubscribe

unsubscribe(channelName)

If a Subscription exists for this name it will be left gracefully and closed. The subscription is removed from the list of PushEx subscriptions so that calling subscribe/1 again will create a new Subscription. This resolves a promise because you are not guaranteed the Subscription is closed until a message is delivered to the server.

Pushex#getExistingSubscription

getExistingSubscription(channelName)

Returns the subscription if it already exists. Does not create a new subscription.

Subscription#bind

bind(eventName, callbackFn)

A subscription channel can have events bound to it, with the callbackFn being invoked when a message is received from the server.

In order to capture all events on a given cahnnel, use the special channelName '*'. The callbackFn will be invoked on every channel event, regardless of the name.

The callbackFn is invoked as callbackFn(data, event).

bind returns a function which can be used to unsubscribe the event:

const unsub = pushex.subscribe('myCh').bind('*', myCallback)
// myCallback is invoked when myCh receives any event
unsub()
// myCallback will not be invoked through this callback

Subscription#unbind

unbindAll(eventName)

All event bindings for the provided name will be removed. The Subscription still remains open and alive, so you could rebind events and it would work.

Subscription#unbindAll

unbindAll()

All event bindings for the entire subscription will be removed. The Subscription still remains open and alive, so you could rebind events and it would work.

Subscription#hasBindings

hasBindings()

Returns true/false indicating whether there are active bindings for this subscription.