CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pusher-js

Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

Pusher Channels JavaScript

Pusher Channels JavaScript library enables real-time bidirectional communication between clients and servers through WebSocket connections with automatic fallback to HTTP polling. It provides pub/sub messaging, presence channels, user authentication, and client-side event triggering across web browsers, React Native, Node.js, and web workers.

Package Information

  • Package Name: pusher-js
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install pusher-js

Core Imports

import Pusher from "pusher-js";
import { Channel, PresenceChannel, Members } from "pusher-js";

For CommonJS:

const Pusher = require("pusher-js");

For Node.js specifically:

const Pusher = require("pusher-js/dist/node/pusher");

Basic Usage

import Pusher from "pusher-js";

// Initialize Pusher client
const pusher = new Pusher("your-app-key", {
  cluster: "us2",
  forceTLS: true
});

// Subscribe to a channel
const channel = pusher.subscribe("my-channel");

// Bind to events
channel.bind("my-event", (data) => {
  console.log("Received data:", data);
});

// Global event binding
pusher.bind("connection_established", () => {
  console.log("Connected to Pusher");
});

// Connect and disconnect
pusher.connect();
// pusher.disconnect();

Architecture

Pusher-js is built around several key components:

  • Pusher Client: Main client class managing connections and global events
  • Channel System: Public, private, and presence channels for different messaging patterns
  • Connection Manager: WebSocket management with automatic reconnection and transport fallback
  • Authentication System: Channel authorization and user authentication for private/presence channels
  • Runtime Abstraction: Platform-specific implementations for web, Node.js, React Native, and web workers
  • Event System: Comprehensive event binding and dispatching throughout the library

Capabilities

Client Management

Core Pusher client functionality for establishing connections, managing global events, and controlling the overall WebSocket connection lifecycle.

class Pusher {
  constructor(app_key: string, options: Options);
  connect(): void;
  disconnect(): void;
  bind(event_name: string, callback: Function, context?: any): Pusher;
  unbind(event_name?: string, callback?: Function, context?: any): Pusher;
  bind_global(callback: Function): Pusher;
  unbind_global(callback?: Function): Pusher;
  unbind_all(callback?: Function): Pusher;
  send_event(event_name: string, data: any, channel?: string): boolean;
  signin(): void;
  shouldUseTLS(): boolean;
}

interface Options {
  cluster: string;
  authEndpoint?: string;
  authTransport?: AuthTransport;
  channelAuthorization?: ChannelAuthorizationOptions;
  userAuthentication?: UserAuthenticationOptions;
  forceTLS?: boolean;
  enableStats?: boolean;
  disabledTransports?: Transport[];
  enabledTransports?: Transport[];
  activityTimeout?: number;
  pongTimeout?: number;
  unavailableTimeout?: number;
  wsHost?: string;
  wsPort?: number;
  wssPort?: number;
  httpHost?: string;
  httpPort?: number;
  httpsPort?: number;
  nacl?: any;
}

Client Management

Channel Communication

Channel-based pub/sub messaging system supporting public channels, private authenticated channels, and presence channels with member tracking.

subscribe(channel_name: string): Channel;
unsubscribe(channel_name: string): void;
channel(name: string): Channel;
allChannels(): Channel[];

class Channel {
  constructor(name: string, pusher: Pusher);
  bind(event: string, callback: Function, context?: any): Channel;
  unbind(event?: string, callback?: Function, context?: any): Channel;
  trigger(event: string, data: any): boolean;
  subscribe(): void;
  unsubscribe(): void;
}

class PresenceChannel extends Channel {
  members: Members;
}

class Members {
  count: number;
  myID: any;
  me: any;
  get(id: string): any;
  each(callback: Function): void;
}

Channel Communication

Authentication

Authentication and authorization system for private channels, presence channels, and user authentication with customizable auth handlers.

interface ChannelAuthorizationOptions {
  transport: 'ajax' | 'jsonp';
  endpoint: string;
  params?: any;
  headers?: any;
  paramsProvider?: () => any;
  headersProvider?: () => any;
  customHandler?: ChannelAuthorizationHandler;
}

interface UserAuthenticationOptions {
  transport: 'ajax' | 'jsonp';
  endpoint: string;
  params?: any;
  headers?: any;
  paramsProvider?: () => any;
  headersProvider?: () => any;
  customHandler?: UserAuthenticationHandler;
}

type ChannelAuthorizationHandler = (
  params: ChannelAuthorizationRequestParams,
  callback: ChannelAuthorizationCallback
) => void;

type UserAuthenticationHandler = (
  params: UserAuthenticationRequestParams,
  callback: UserAuthenticationCallback
) => void;

Authentication

Connection Management

Low-level connection management with WebSocket handling, automatic reconnection, transport fallback, and connection state monitoring.

class ConnectionManager {
  state: string;
  connection: Connection;
  socket_id: string;
  connect(): void;
  disconnect(): void;
  send(data: any): boolean;
  send_event(name: string, data: any, channel?: string): boolean;
  isUsingTLS(): boolean;
}

interface ConnectionCallbacks {
  message?: (event: any) => void;
  ping?: () => void;
  pong?: () => void;
  error?: (error: any) => void;
  closed?: () => void;
}

Connection Management

Error Types

class BadEventName extends Error {
  constructor(msg?: string);
}

class BadChannelName extends Error {
  constructor(msg?: string);
}

class HTTPAuthError extends Error {
  status: number;
  constructor(status: number, msg?: string);
}

class RequestTimedOut extends Error {
  constructor(msg?: string);
}

class TransportClosed extends Error {
  constructor(msg?: string);
}

class UnsupportedFeature extends Error {
  constructor(msg?: string);
}

Platform Support

Pusher-js supports multiple JavaScript environments:

  • Web Browsers: Full WebSocket and HTTP polling support with JSONP fallback
  • Node.js: Server-side WebSocket client for backend applications
  • React Native: Mobile-optimized implementation with network state detection
  • Web Workers: Background thread support for web applications

Each platform has optimized transport layers and runtime-specific features while maintaining the same core API.

Install with Tessl CLI

npx tessl i tessl/npm-pusher-js
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/pusher-js@8.4.x
Publish Source
CLI
Badge
tessl/npm-pusher-js badge