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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-native-websocket-service

v1.1.0

Published

A reusable WebSocket service with reconnect, heartbeat, and React Native support.

Readme

WebSocketService

A reusable WebSocket service for React Native with:

  • Auto-reconnect (with exponential backoff)
  • Heartbeat messages
  • Connection timeout handling
  • AppState awareness (pause/resume connections when app is backgrounded/foregrounded)
  • TypeScript support

This service is generic enough for real-time data updates, chat applications, and more.


Installation

Since this library depends on React Native’s AppState, it requires react-native in your project.

npm install websocket-service

In your package.json, react-native will be a peer dependency, so make sure it’s already installed in your project.

Usage 

import { WebSocketService } from '@yourname/websocket-service';

const ws = new WebSocketService(
  {
    url: 'wss://example.com/socket',
    heartbeatIntervalMs: 15000, // optional
    maxRetries: 5,              // optional
  },
  {
    onConnected: () => console.log("✅ Connected"),
    onDisconnected: () => console.log("❌ Disconnected"),
    onMessage: (msg) => console.log("💬 Received:", msg),
    onError: (err) => console.error("Error:", err),
  }
);

// Sending data
ws.send({ type: 'chat', text: 'Hello World!' });

// Manually disconnect
ws.disconnect();

// Force reconnect
ws.forceReconnect();

// Destroy completely (no reconnects)
ws.destroy();


API
Constructor

new WebSocketService(config: WebSocketConfig, callbacks?: WebSocketCallbacks)


WebSocketConfig
Field	Type	Default	Description
url	string	Required	The WebSocket server URL
maxRetries	number	5	Max reconnection attempts
initialBackoffMs	number	1000	Initial delay before reconnect (ms)
connectionTimeoutMs	number	10000	Time to wait for connection before timing out
heartbeatIntervalMs	number	30000	Interval to send heartbeat messages
WebSocketCallbacks
Callback	Parameters	Description
onConnected	()	Called when connection opens
onDisconnected	()	Called when connection closes
onMessage	(message: string)	Called when a message is received
onError	(error: Error)	Called on connection error
Methods
Method	Description
connect()	Manually initiate/retry a connection
disconnect()	Close connection without retry
send(data)	Send JSON data to server
forceReconnect()	Immediately close and reconnect
destroy()	Close connection and remove all listeners
isConnected (getter)	boolean – current connection state
url (getter)	string – current URL
readyState (getter)	WebSocket ready state number
Example: Sports Data Feed
const sportsSocket = new WebSocketService(
  { url: 'wss://sports.example.com/live' },
  {
    onMessage: (msg) => {
      const data = JSON.parse(msg);
      console.log("🏀 Live update:", data);
    }
  }
);

Example: Chat
const chatSocket = new WebSocketService(
  { url: 'wss://chat.example.com' },
  {
    onConnected: () => console.log("Chat connected"),
    onMessage: (msg) => console.log("Chat:", msg)
  }
);

chatSocket.send({ type: 'message', user: 'Abrham', text: 'Hi!' });

License

MIT