# @jmoxey/pricehub.js

> Pricehub.js is a JavaScript Client SDK to provide an interface to subscribe to real-time changes from Pricehub.

Latest version **1.0.5** (published 2021-04-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jmoxey/pricehub.js
pnpm add @jmoxey/pricehub.js
yarn add @jmoxey/pricehub.js
bun add @jmoxey/pricehub.js
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2021-04-06 |
| First published | 2021-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 41.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jake Moxey |
| Maintainers | jmoxey |

## Links

- npm: https://www.npmjs.com/package/@jmoxey/pricehub.js
- npm.io page: https://npm.io/package/@jmoxey/pricehub.js

## Dependencies (1)

- [phoenix-channels](https://npm.io/package/phoenix-channels.md) ^1.0.0

## Recent versions

- 1.0.5 (latest) — 2021-04-06
- 1.0.4 — 2021-04-05
- 1.0.3 — 2021-03-26
- 1.0.2 — 2021-03-26
- 1.0.1 — 2021-03-26
- 1.0.0 — 2021-03-26

## README

# Pricehub.js

Pricehub.js is a JavaScript Client SDK to provide an interface to subscribe to real-time changes from Pricehub.

## Install

```
npm i pricehub.js
```

## Usage

To subscribe to a currency pair channel, all you have to do is call `pricehub.subscribe` with a channel ID and a set of callbacks.

In the below example, we are subscribing to the BTC/AUD currency pair.

```js
import pricehub from 'pricehub.js';
// const pricehub = require('pricehub.js');

const { dispose } = pricehub.subscribe('rate:BTC-AUD', {
  onJoined: () => console.log('i have joined!'),
  onData: payload => console.log(`Reference: ${payload.r}, Ask: ${payload.a}, Bid: ${payload.b}`),
  onError: err => console.log(`Oh noes! ${err.message}`)
});

// Whenever you are finished with the subscription, invoke `dispose`:
dispose();
```

- When we join the channel, `onJoined` will be invoked.
- When we receive data from the channel, `onData` will be invoked.
- If we receive an error from the channel, `onError` will be invoked.
- If an `interval` is specified ([see API below](#api)), this means that `onData` will only be invoked every `x` milliseconds.

> Note: You don't have to worry about managing subscriptions to a channel as Pricehub.js caches the channel under-the-hood, and reuses that channel if the consumer attempts to subscribe to the same channel. This means if you have 5 subscriptions to `rate:BTC-AUD`, there will only ever be one active channel (whenever data is recieved from the socket, Pricehub.js will broadcast the changes to your subscribers).

## API

```js
const { dispose } = pricehub.subscribe(
  channelId: string, 
  {
    onJoined: () => void,      // Callback to invoke when channel has been joined.
    onData: (payload) => void, // Callback to invoke when data has been received.
    onError: (err) => void,    // Callback to invoke when an error has occurred.
    interval: number,          // (Optional) Interval in ms. Defaults to 0 — no interval
  }
)
```

---
_Source: https://npm.io/package/@jmoxey/pricehub.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
