CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-laravel-echo

Laravel Echo library for beautiful Pusher and Socket.IO integration

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

echo-interface.mddocs/

Echo Interface

Main Echo class providing connection management and channel access methods for WebSocket broadcasting services.

Capabilities

Echo Constructor

Creates new Echo instance with broadcaster-specific configuration.

/**
 * Create a new Echo instance
 * @param options - Configuration options for the specific broadcaster
 */
constructor(options: EchoOptions<T>);

Usage Examples:

import Echo from "laravel-echo";

// Pusher configuration
const echo = new Echo({
  broadcaster: "pusher",
  key: "your-pusher-key",
  cluster: "us2",
  authEndpoint: "/broadcasting/auth",
});

// Socket.IO configuration
const echo = new Echo({
  broadcaster: "socket.io",
  host: window.location.hostname + ":6001",
});

// Reverb configuration
const echo = new Echo({
  broadcaster: "reverb",
  key: "your-reverb-key",
  wsHost: "localhost",
  wsPort: 8080,
});

Channel Access Methods

Get channel instances for different channel types.

/**
 * Get a public channel instance by name
 * @param channel - Channel name
 * @returns Public channel instance
 */
channel(channel: string): Broadcaster[T]["public"];

/**
 * Get a private channel instance by name
 * @param channel - Channel name
 * @returns Private channel instance
 */
private(channel: string): Broadcaster[T]["private"];

/**
 * Get an encrypted private channel instance by name
 * @param channel - Channel name
 * @returns Encrypted private channel instance
 * @throws Error if broadcaster doesn't support encrypted channels
 */
encryptedPrivate(channel: string): Broadcaster[T]["encrypted"];

/**
 * Get a presence channel instance by name
 * @param channel - Channel name
 * @returns Presence channel instance
 */
join(channel: string): Broadcaster[T]["presence"];

Usage Examples:

// Public channel
echo.channel("orders").listen("OrderShipped", (e) => {
  console.log(e.order);
});

// Private channel
echo.private("user.1").listen("MessageSent", (e) => {
  console.log(e.message);
});

// Encrypted private channel (Pusher/Reverb only)
echo.encryptedPrivate("sensitive-data").listen("DataUpdate", (e) => {
  console.log(e.data);
});

// Presence channel
echo.join("chat.1")
  .here((users) => console.log("Current users:", users))
  .joining((user) => console.log("User joined:", user))
  .leaving((user) => console.log("User left:", user));

Event Listening

Listen for events on channels with callback functions.

/**
 * Listen for an event on a channel instance
 * @param channel - Channel name
 * @param event - Event name to listen for
 * @param callback - Function to call when event is received
 * @returns The channel instance for chaining
 */
listen(channel: string, event: string, callback: CallableFunction): Broadcaster[T]["public"];

Usage Examples:

// Listen for specific events
echo.listen("orders", "OrderShipped", (e) => {
  console.log("Order shipped:", e.order.id);
});

// Listen for Laravel notifications
echo.private("App.Models.User.1").notification((notification) => {
  console.log("Notification:", notification.type);
});

Channel Management

Methods for managing channel subscriptions and connections.

/**
 * Leave the given channel, as well as its private and presence variants
 * @param channel - Channel name to leave
 */
leave(channel: string): void;

/**
 * Leave the given channel specifically
 * @param channel - Channel name to leave
 */
leaveChannel(channel: string): void;

/**
 * Leave all subscribed channels
 */
leaveAllChannels(): void;

Usage Examples:

// Leave specific channel and its variants
echo.leave("chat.1"); // Leaves chat.1, private-chat.1, presence-chat.1

// Leave specific channel only
echo.leaveChannel("presence-chat.1");

// Leave all channels
echo.leaveAllChannels();

Connection Management

Control WebSocket connection lifecycle.

/**
 * Create a new connection to the WebSocket service
 */
connect(): void;

/**
 * Disconnect from the Echo server
 */
disconnect(): void;

/**
 * Get the Socket ID for the current connection
 * @returns Socket ID string or undefined if not connected
 */
socketId(): string | undefined;

Usage Examples:

// Manual connection control
echo.connect();

// Get socket ID for API requests
const socketId = echo.socketId();
if (socketId) {
  // Include in API requests for event tracking
  fetch("/api/data", {
    headers: {
      "X-Socket-ID": socketId,
    },
  });
}

// Disconnect when done
echo.disconnect();

HTTP Interceptors

Automatic HTTP request interceptor registration for popular libraries.

/**
 * Register all available HTTP interceptors
 */
registerInterceptors(): void;

/**
 * Register Vue.js HTTP interceptor to add X-Socket-ID header
 */
registerVueRequestInterceptor(): void;

/**
 * Register Axios HTTP interceptor to add X-Socket-ID header
 */
registerAxiosRequestInterceptor(): void;

/**
 * Register jQuery AJAX interceptor to add X-Socket-ID header
 */
registerjQueryAjaxSetup(): void;

/**
 * Register Turbo request interceptor to add X-Socket-ID header
 */
registerTurboRequestInterceptor(): void;

Usage Examples:

// Disable automatic interceptors
const echo = new Echo({
  broadcaster: "pusher",
  key: "your-key",
  withoutInterceptors: true,
});

// Register specific interceptors manually
echo.registerAxiosRequestInterceptor();

// All interceptors are automatically registered by default
// They add X-Socket-ID header to HTTP requests for Laravel integration

Configuration Options

Each broadcaster has specific configuration requirements:

Pusher/Reverb Configuration

interface PusherOptions {
  broadcaster: "pusher" | "reverb";
  key: string;
  cluster?: string;
  wsHost?: string;
  wsPort?: number;
  wssPort?: number;
  forceTLS?: boolean;
  enabledTransports?: string[];
  disabledTransports?: string[];
}

Socket.IO Configuration

interface SocketIoOptions {
  broadcaster: "socket.io";
  host?: string;
  namespace?: string;
}

Ably Configuration

interface AblyOptions {
  broadcaster: "ably";
  key: string;
  clientId?: string;
}

docs

channels.md

connectors.md

echo-interface.md

index.md

tile.json