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 🙏

© 2026 – Pkg Stats / Ryan Hefner

metal-price-live

v1.0.0

Published

Live metal price hook for react and react native using socket.io

Downloads

511

Readme

metal-price-live

Live metal prices for React and React Native via Socket.IO.

Installation

Using npm:

npm install metal-price-live

Using yarn:

yarn add metal-price-live

socket.io-client is bundled as a package dependency, so you do not need to install it separately.

1.0.0 marks the Socket.IO-based hook contract shown below as the stable public API for the package.

Usage

import useMetalPriceLive from 'metal-price-live';

// apiKey, url, and options (with path + event) are required.
const { status, data, error } = useMetalPriceLive(
  'your-api-key',
  'https://your-pricefeed.example.com',
  {
    path: '/api/v1/your-service/socket.io',
    event: 'your:event-name',
  }
);

if (status === 'connected') {
  const gold = data.XAUUSD; // { bid, ask, high, low, ... }
  const silver = data.XAGUSD;
}

If any required input (apiKey, url, options.path, or options.event) is missing or blank, the hook returns { status: 'error', error: '<field> is required' } and does not open a socket.

Migrating from 0.3.x / 0.4.x? The stable 1.0.0 API uses a new hook signature. The old hook took useMetalPriceLive(socketUrl, apiKey) (raw WebSocket). It now takes useMetalPriceLive(apiKey, url, options) over Socket.IO — note the first two arguments are reordered (apiKey is now first) and the options object with a required path and event is new. A straight upgrade without changing your call site no longer crashes during render, but it will settle into status: 'error' until you swap the argument order and provide the required options object.

Optional knobs

const state = useMetalPriceLive(
  'your-api-key',
  'https://your-pricefeed.example.com',
  {
    path: '/api/v1/your-service/socket.io', // required
    event: 'your:event-name', // required
    headerName: 'x-api-key', // default: 'x-api-key'
    // transports: ['polling', 'websocket'], // optional override

    // Reconnection knobs (forwarded to socket.io-client). All optional.
    reconnectionAttempts: 10, // default: Infinity
    reconnectionDelay: 1000, // ms, default: 1000
    reconnectionDelayMax: 5000, // ms, default: 5000
    timeout: 20000, // ms, per-attempt handshake timeout
  }
);

Options

| Option | Type | Default | Purpose | | ---------------------- | ------------------------------------------------ | --------------------------------------------------------------- | --------------------------------------------------------------------------------------- | | path | string | — (required) | Socket.IO mount path on the server. | | event | string | — (required) | Event name carrying price snapshots. | | headerName | string | x-api-key | HTTP header name the API key is sent under (polling handshake only — see browser note). | | transports | ('websocket' \| 'polling' \| 'webtransport')[] | undefined (Socket.IO default: polling → upgrade to websocket) | Transport order. | | reconnectionAttempts | number | Infinity | Max reconnect attempts before emitting reconnect_failed. | | reconnectionDelay | number (ms) | 1000 | Initial backoff. | | reconnectionDelayMax | number (ms) | 5000 | Backoff cap. | | timeout | number (ms) | 20000 | Per-attempt handshake timeout. |

Connection state

The hook returns the current connection state (a discriminated union) plus a stable reconnect() method:

type ConnectionState =
  | { status: 'connecting'; data?: ApiData }
  | { status: 'connected'; data: ApiData }
  | { status: 'error'; data?: ApiData; error: string };

// Returned value: ConnectionState & { reconnect: () => void }

State transitions:

  • 'connecting' — initial state, and the state during a network drop while Socket.IO retries in the background. The last data snapshot stays available.
  • 'connected' — set when the first valid tick event arrives, and re-set on 'connect' if a cached snapshot is available (so reconnects don't blank the UI while waiting for the next tick).
  • 'error' — set on connect_error with the underlying message; on reconnect_failed (terminal — only fires if you set a finite reconnectionAttempts) with error: 'reconnect_failed'; and on an 'io server disconnect' (the server forcibly closed the socket, which Socket.IO does not auto-reconnect from) with error: 'io server disconnect'. A server-emitted application error event surfaces the same way — its payload is normalized to a string (Socket.IO sends an Error here, but a custom server may send anything), falling back to 'Socket connection error'; a subsequent valid tick clears it back to 'connected'. The last data snapshot is preserved in all cases.

The two terminal errors — 'io server disconnect' and 'reconnect_failed' — are not retried by Socket.IO. Call reconnect() (below) to recover.

Manual reconnect

The returned reconnect() tears down the current socket and starts a fresh connection from 'connecting'. It's the way to recover from the terminal error states ('io server disconnect' / 'reconnect_failed'), which Socket.IO does not retry on its own:

const { status, error, reconnect } = useMetalPriceLive(apiKey, url, options);

if (status === 'error') {
  return <Button title={`Retry (${error})`} onPress={reconnect} />;
}

reconnect() is stable across renders (safe to pass as a prop or use in a dependency array) and also works mid-connection if you ever need to force a clean reconnect.

Payload shape

interface MetalQuote {
  symbol: string;
  name: string;
  bid: number;
  ask: number;
  high: number;
  low: number;
  tickTimestamp: string;
  timestamp: number;
}

interface ApiData {
  timestamp: string;
  stale: boolean;
  XAUUSD: MetalQuote;
  XAGUSD: MetalQuote;
  [symbol: string]: MetalQuote | string | boolean;
}

The hook accepts both wrapped ({ event, data: ApiData }) and unwrapped (ApiData) tick payloads, and silently ignores anything that fails validation. A payload is accepted only when timestamp is a string, stale is a boolean, and both XAUUSD and XAGUSD are full quote objects (every MetalQuote field present and correctly typed). This guarantees data.XAUUSD.bid etc. are real numbers once status === 'connected'.

Browser compatibility note

The API key is sent in the x-api-key HTTP header via Socket.IO's extraHeaders option. This works as expected in:

  • Node.js
  • React Native (RN's WebSocket / XHR honor custom headers)
  • Browsers — but only on the polling transport during the Socket.IO handshake (which is the default — Socket.IO starts with polling and then upgrades to WebSocket)

The browser's native WebSocket API does not allow custom headers. If you force a WebSocket-only transport in a browser (transports: ['websocket']), the x-api-key header will not be transmitted and the connection will fail authentication. Either leave transports unset (recommended) or have the server also accept the API key via auth / query string for browser-only WebSocket usage.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library