Pusher Channels JavaScript library for browsers, React Native, NodeJS and web workers
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Channel-based pub/sub messaging system supporting public channels, private authenticated channels, and presence channels with member tracking.
Subscribe to and manage channels for receiving real-time messages.
/**
* Subscribe to a channel
* @param channel_name - Name of the channel to subscribe to
* @returns Channel instance for event binding
*/
subscribe(channel_name: string): Channel;
/**
* Unsubscribe from a channel
* @param channel_name - Name of the channel to unsubscribe from
*/
unsubscribe(channel_name: string): void;
/**
* Get a channel by name (must be already subscribed)
* @param name - Name of the channel
* @returns Channel instance or null if not found
*/
channel(name: string): Channel;
/**
* Get all subscribed channels
* @returns Array of all Channel instances
*/
allChannels(): Channel[];Subscription Examples:
// Subscribe to public channel
const publicChannel = pusher.subscribe("news-updates");
// Subscribe to private channel (requires authentication)
const privateChannel = pusher.subscribe("private-user-123");
// Subscribe to presence channel (requires authentication + user info)
const presenceChannel = pusher.subscribe("presence-chat-room");
// Get existing channel
const channel = pusher.channel("news-updates");
// Get all channels
const channels = pusher.allChannels();
console.log(`Subscribed to ${channels.length} channels`);
// Unsubscribe
pusher.unsubscribe("news-updates");Base channel class for pub/sub messaging and event handling.
/**
* Base channel class for pub/sub messaging
* Extends EventsDispatcher for event binding capabilities
*/
class Channel extends EventsDispatcher {
// Properties
name: string;
pusher: Pusher;
subscribed: boolean;
subscriptionPending: boolean;
subscriptionCancelled: boolean;
subscriptionCount: null;
constructor(name: string, pusher: Pusher);
// Event Binding
bind(event: string, callback: Function, context?: any): Channel;
unbind(event?: string, callback?: Function, context?: any): Channel;
bind_global(callback: Function): Channel;
unbind_global(callback?: Function): Channel;
unbind_all(): Channel;
// Client Events
trigger(event: string, data: any): boolean;
// Subscription Management
subscribe(): void;
unsubscribe(): void;
cancelSubscription(): void;
reinstateSubscription(): void;
disconnect(): void;
// Authorization (overridden in subclasses)
authorize(socketId: string, callback: ChannelAuthorizationCallback): void;
}Channel Usage Examples:
const channel = pusher.subscribe("my-channel");
// Bind to specific events
channel.bind("new-message", (data) => {
console.log("New message:", data.message);
});
channel.bind("user-joined", (data) => {
console.log(`${data.username} joined`);
});
// Bind to all events on this channel
channel.bind_global((event, data) => {
console.log(`Channel event: ${event}`, data);
});
// Trigger client events (requires client event permissions)
channel.trigger("client-typing", {
user: "alice",
typing: true
});
// Unbind specific events
channel.unbind("new-message", messageHandler);
// Unbind all events
channel.unbind_all();Private channel requiring authentication for subscription.
/**
* Private channel requiring authentication
* Extends Channel with authorization capabilities
*/
class PrivateChannel extends Channel {
constructor(name: string, pusher: Pusher);
// Override Channel authorization with actual auth logic
authorize(socketId: string, callback: ChannelAuthorizationCallback): void;
}Private Channel Examples:
const privateChannel = pusher.subscribe("private-user-messages");
// Same API as regular channels, but requires authentication
privateChannel.bind("new-message", (data) => {
console.log("Private message:", data);
});
// Authentication handled automatically during subscription
privateChannel.bind("pusher:subscription_succeeded", () => {
console.log("Successfully authenticated to private channel");
});
privateChannel.bind("pusher:subscription_error", (error) => {
console.error("Failed to authenticate:", error);
});Extended channel with member presence tracking capabilities.
/**
* Presence channel with member tracking
* Extends PrivateChannel (which extends Channel)
*/
class PresenceChannel extends PrivateChannel {
members: Members;
constructor(name: string, pusher: Pusher);
// Inherited from Channel + PrivateChannel
// Plus presence-specific authorization
authorize(socketId: string, callback: Function): void;
}Presence Channel Examples:
const presenceChannel = pusher.subscribe("presence-chat-room") as PresenceChannel;
// Access member information
console.log(`${presenceChannel.members.count} users online`);
console.log("My ID:", presenceChannel.members.myID);
console.log("My data:", presenceChannel.members.me);
// Iterate over all members
presenceChannel.members.each((member) => {
console.log("Member:", member);
});
// Get specific member
const user = presenceChannel.members.get("user-123");
// Presence events
presenceChannel.bind("pusher:subscription_succeeded", (members) => {
console.log("Joined presence channel with members:", members);
});
presenceChannel.bind("pusher:member_added", (member) => {
console.log("Member joined:", member);
});
presenceChannel.bind("pusher:member_removed", (member) => {
console.log("Member left:", member);
});Manages the member list for presence channels.
/**
* Manages presence channel member list
*/
class Members {
members: any; // Object containing member data
count: number; // Number of members
myID: any; // Current user's ID
me: any; // Current user's data
constructor();
/**
* Get member by ID
* @param id - Member ID to retrieve
* @returns Member data or undefined
*/
get(id: string): any;
/**
* Iterate over all members
* @param callback - Function called for each member
*/
each(callback: Function): void;
/**
* Set current user's ID
* @param id - User ID to set
*/
setMyID(id: string): void;
// Internal methods (not typically used directly)
onSubscription(subscriptionData: any): void;
addMember(memberData: any): any;
removeMember(memberData: any): any;
reset(): void;
}Members Usage Examples:
const presenceChannel = pusher.subscribe("presence-lobby") as PresenceChannel;
const members = presenceChannel.members;
// Check member count
if (members.count > 10) {
console.log("Lobby is getting crowded!");
}
// Get current user info
console.log("I am:", members.me);
// Find specific member
const admin = members.get("admin-user-id");
if (admin) {
console.log("Admin is online:", admin);
}
// List all members
members.each((member) => {
console.log(`Member ${member.id}: ${member.info.name}`);
});Different channel types have specific naming conventions and behaviors:
// Channel type examples
type ChannelName =
| string // Public channel: "news", "updates"
| `private-${string}` // Private channel: "private-user-123"
| `presence-${string}`; // Presence channel: "presence-chat-room"Channel Type Examples:
// Public channels (no authentication required)
const news = pusher.subscribe("news-feed");
const alerts = pusher.subscribe("system-alerts");
// Private channels (authentication required)
const userChannel = pusher.subscribe("private-user-456");
const adminChannel = pusher.subscribe("private-admin-panel");
// Presence channels (authentication + user info required)
const chatRoom = pusher.subscribe("presence-general-chat");
const gameRoom = pusher.subscribe("presence-game-lobby-1");pusher:subscription_succeeded - Successfully subscribed to channelpusher:subscription_error - Failed to subscribe (auth failure, etc.)pusher:subscription_count - Channel subscription count updatepusher:member_added - New member joined the presence channelpusher:member_removed - Member left the presence channelpusher:error - Channel-specific error occurred// Public channel subscription
interface SubscriptionSucceededData {
// No additional data for public channels
}
// Presence channel subscription
interface PresenceSubscriptionData {
presence: {
count: number;
ids: string[];
hash: { [id: string]: any };
};
}interface MemberEventData {
id: string;
info: any; // User-defined member information
}Event Data Examples:
// Subscription succeeded
channel.bind("pusher:subscription_succeeded", (data) => {
console.log("Subscribed successfully", data);
});
// Presence subscription with member data
presenceChannel.bind("pusher:subscription_succeeded", (data) => {
console.log(`Joined with ${data.presence.count} members`);
console.log("Member IDs:", data.presence.ids);
console.log("Member data:", data.presence.hash);
});
// Member events
presenceChannel.bind("pusher:member_added", (member) => {
console.log(`${member.info.name} (${member.id}) joined`);
});
presenceChannel.bind("pusher:member_removed", (member) => {
console.log(`${member.info.name} (${member.id}) left`);
});Install with Tessl CLI
npx tessl i tessl/npm-pusher-js