MessageBus JavaScript Client

4.5.2 · active · verified Tue Apr 21

The `message-bus-client` package is the official JavaScript client library for connecting to the Ruby `message_bus` server. It provides robust functionalities for subscribing to channels, receiving real-time messages via polling, long-polling, or long-polling with streaming, and managing subscriptions and message backlogs. Designed to work seamlessly with the `message_bus` Ruby gem, which acts as a Rack middleware, it enables scalable concurrent connections. The current stable version is 4.5.2, reflecting active maintenance, as it is a critical component of Discourse, where it's used in production at scale. Its primary role is to establish a reliable client-server communication channel for web applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the MessageBus client, subscribe to multiple channels, and receive real-time messages. It includes optional server URL configuration.

import { MessageBus } from 'message-bus-client';

// Configure the MessageBus client with your server's base URL if different from current origin
const bus = new MessageBus({
  baseUrl: 'http://localhost:9292/message-bus' // Replace with your MessageBus server URL
});

bus.start(); // Start polling for messages immediately

bus.subscribe('/global-channel', (data, messageId) => {
  console.log(`Received message on /global-channel (ID: ${messageId}):`, data);
});

bus.subscribe('/user-channel-123', (data) => {
  console.log('Received targeted message:', data);
});

// To stop listening and disconnect
// setTimeout(() => {
//   bus.unsubscribe('/global-channel');
//   bus.stop();
//   console.log('MessageBus stopped and unsubscribed.');
// }, 60000);

view raw JSON →