Client SDK

Realtime WebSocket Notifications for React

Add WebSocket channels and real-time notifications to any React app. Subscribe in useEffect, bind events declaratively, clean up automatically — works with Vite, Create React App, and Remix.

npm install @apinator/client
Client

Adding real-time WebSocket features to a React app doesn't require a complex server setup. Apinator's client SDK opens a persistent WebSocket connection from the browser and delivers events directly to your components. Subscribe in useEffect, bind event handlers, and let the SDK handle reconnection — your React state stays in sync with live data automatically.

Quick Start

How to add WebSocket notifications to React

Add realtime features to your React app in a few steps.

01

Install the client SDK

Add the WebSocket client SDK to your React project. Works with Vite, Create React App, Next.js, and Remix.

npm install @apinator/client
02

See your first event (no backend)

No server code yet: subscribe to the public channel my-channel (public channels need no auth endpoint) with your app key and cluster (us or eu) from the dashboard, then click the Send test event button on your app's Overview page in the dashboard. The event prints in your console. Open the dashboard

// Inside any component
import { useEffect } from 'react'
import { Apinator } from '@apinator/client'

useEffect(() => {
  const client = new Apinator({ appKey: 'YOUR_APP_KEY', cluster: 'us' }).connect()
  client.subscribe('my-channel').bind('my-event', (data) => console.log('Received:', data))
  return () => { client.disconnect() }
}, [])
03

Create a client instance

Initialize the client with your app key. Set an auth endpoint for private channels.

// lib/realtime.ts
import { Apinator } from '@apinator/client'

export const client = new Apinator({
  appKey: 'your-app-key',
  cluster: 'us',
  authEndpoint: '/api/realtime/auth'
})
04

Subscribe to a WebSocket channel

Open a persistent WebSocket connection and subscribe inside useEffect. The SDK handles reconnection with exponential backoff automatically.

useEffect(() => {
  const channel = client
    .connect()
    .subscribe('chat-room')

  channel.bind('message', (data) => {
    setMessages(prev => [...prev, data])
  })

  return () => client.unsubscribe('chat-room')
}, [])
05

Trigger from your backend

Use any server SDK (Node.js, Python, Go, PHP) to publish events. Clients receive them instantly.

// Your backend (any language)
await realtime.trigger({
  name: 'message',
  channel: 'chat-room',
  data: JSON.stringify({ text: 'Hello!' })
})

Why React

Built for React developers

useEffect Friendly

Subscribe on mount, unsubscribe on cleanup. Fits naturally into React component lifecycles without memory leaks.

Auto-Reconnect

Built-in exponential backoff reconnection. Your UI stays connected even through network interruptions.

Presence Channels

Track who is online with presence channels. Show user avatars, typing indicators, and live cursors.

Tiny Bundle

Lightweight client with zero dependencies. Tree-shakeable ESM build keeps your React app fast.

How WebSocket channels work in React

React's component model is a natural fit for WebSocket subscriptions. The useEffect hook maps directly to the connect/subscribe/bind lifecycle, and returning a cleanup function that calls unsubscribe() removes the channel and its handlers when the component unmounts. The challenge is getting this right — forgetting to unsubscribe can cause memory leaks and duplicate event handlers, especially with React Strict Mode's double-invocation in development.

Apinator's client SDK handles the tricky parts. A single Apinator instance manages the underlying WebSocket connection, so subscribing from multiple components at once doesn't open multiple connections — subscriptions are multiplexed over one connection. unsubscribe() also drops every handler bound to that channel and is safe to call even if the channel is already gone, making cleanup straightforward. The SDK also handles reconnection automatically with exponential backoff, so temporary network interruptions don't require any error-handling code in your components.

The result is that adding a real-time WebSocket notification feed to a React component takes about 10 lines of code in useEffect. No server-side WebSocket infrastructure required — Apinator manages the persistent connection layer entirely.

Full Example

Full Example: Realtime WebSocket Chat Component in React

Complete WebSocket example: a chat component that subscribes to a private channel and renders incoming messages in real-time.

lib/realtime.ts
import { Apinator } from '@apinator/client'

export const client = new Apinator({
  appKey: import.meta.env.VITE_REALTIME_KEY,
  cluster: 'us',
  authEndpoint: '/api/realtime/auth'
})

Start building realtime React apps

Completely free. No credit card. No catch.