Autobahn|JS Documentation

repository·master·Indexed 23 days ago

https://github.com/crossbario/autobahn-js

An open-source JavaScript implementation of the Web Application Messaging Protocol (WAMP) v2. It provides asynchronous Remote Procedure Calls (RPC) and Publish/Subscribe (PubSub) patterns over WebSockets for both Node.js and modern browsers. Features include support for JSON, Msgpack, and CBOR serializers, WAMP-CRA authentication, and a promise-based API using when.js.

Tokens
13.8K
Snippets
41
Records
77
Agent score
75%

What's inside Autobahn|JS

  1. Overview of Autobahn|JS features

    master

    Autobahn|JS is a WAMP v2 implementation providing:

    • Asynchronous RPC and PubSub messaging patterns.
    • Transport support: Uses WebSocket or HTTP long-poll.
    • Promise-based API: Uses when.js for promises, supporting progress for WAMP progressive calls.
    • Cross-platform: Runs in modern browsers and Node.js.
    • Minimal footprint: Small size (~250kB source, ~35kB gzipped).
  2. Project Overview and Environment Requirements

    master

    AutobahnJS is a WAMP (Web Application Messaging Protocol) library. The project is currently in a modernization phase, transitioning from legacy build tools to a modern stack.

    Key Environment Requirements:

    • Node.js: Version 22+ is required to support native WebSocket capabilities (removing the need for the ws dependency).
    • Python: Required for the Crossbar.io test router (installed via PyPI into a Python venv).
    • License: The core autobahn package is licensed under MIT, while autobahn-xbr is licensed under Apache-2.0.
  3. Planned Type Safety via JSDoc and .d.ts

    master

    The project is moving toward providing type safety without a full migration to TypeScript. The planned approach (Track A) includes:

    • Generating .d.ts type definition files for both autobahn and autobahn-xbr packages.
    • Adding JSDoc type annotations to existing source code.
    • Using TypeScript in checkJs mode for CI validation.

    Note: The codebase is not being rewritten in TypeScript to minimize risk to existing customer applications.

  4. How Autobahn|JS and WAMP routers work together

    master

    Autobahn|JS does not facilitate direct communication between application components. Instead, it enables components (such as a browser frontend and a Node.js backend) to connect to a WAMP router.

    The router acts as a central hub that performs rule-based routing for application messages (events and RPC calls). No application code runs inside the router itself.

    Recommended routers include Crossbar.io, which is highly compatible with the WAMP v2 protocol.

  5. Manage Registration and Subscription objects

    master

    When performing WAMP operations, you receive objects that allow you to manage the lifecycle of that specific operation.

    Registration Object (from session.register()):

    • registration.id: Registration ID.
    • registration.procedure: Procedure URI.
    • registration.unregister(): Unregisters the procedure.

    Subscription Object (from session.subscribe()):

    • subscription.id: Subscription ID.
    • subscription.topic: Topic URI.
    • subscription.unsubscribe(): Unsubscribes from the topic.

    Publication Object (from session.publish() when acknowledge: true):

    • publication.id: Publication ID.
  6. Understand the AutobahnJS build pipelines

    master

    The project uses different bundling strategies depending on the package being built:

    autobahn (Core Library)

    1. Starts with lib/autobahn.js.
    2. Bundled via browserify with --standalone autobahn to create a standalone bundle.
    3. Minified using google-closure-compiler (mode: SIMPLE, target: ES2018) to produce build/autobahn.min.js.
    4. Compressed using gzip -9 to produce build/autobahn.min.jgz.
    5. Checksums are generated for MD5, SHA1, and SHA256.

    autobahn-xbr (Ethereum/XBR Integration)

    1. Combines lib/autobahn-xbr.js and JSON contracts from lib/contracts/*.json.
    2. Bundled via esbuild (using --bundle and externalizing Node.js built-ins).
    3. Minified via esbuild to produce build/autobahn-xbr.min.js.
    4. Compressed using gzip -9 to produce build/autobahn-xbr.min.jgz.
  7. Handle Connection Open and Close Callbacks

    master

    The autobahn.Connection object provides two primary callbacks for lifecycle management:

    onopen

    Fired when the connection is established and a new WAMP session is created. It receives (session, details) where session is an autobahn.Session instance.

    autobahn.Connection.onopen = function (session, details) {
       // session is an instance of autobahn.Session
    };

    onclose

    Fired when the connection is closed explicitly, lost, or failed to establish. It receives (reason, details).

    Possible reason values:

    • "closed": Explicitly closed. No reconnection attempted.
    • "lost": Connection was established but lost. Automatic reconnection will happen unless you return a truthy value from this callback.
    • "unreachable": Connection could not be established (e.g., invalid URL). No reconnection attempted.
    • "unsupported": No WebSocket transport could be created.

    details contains the reason and message passed to .close(), but is not applicable for "lost" or "unreachable" cases.

    autobahn.Connection.onopen = function (session, details) {
       // Underlying transport to WAMP router established and new WAMP session started.
       // session is an instance of autobahn.Session
    };
    
    autobahn.Connection.onclose = function (reason, details) {
       // connection closed, lost or unable to connect
    };
  8. How WAMP application components and routers work together

    master

    A WAMP-based system is composed of loosely coupled application components (e.g., a browser frontend or a Node.js backend) that connect to a WAMP router.

    The router is a generic component that does not run application code; its sole purpose is to provide routing for events and calls between components. This decoupling allows components to communicate without knowing each other's location or implementation details.

  9. Serve JSON data via Crossbar.io webservice

    master

    You can serve JSON data directly from a Crossbar.io node by configuring a web transport with a path of type json. The value key in the path configuration defines the JSON object that will be returned when that URL is requested.

    {
        "workers": [
            {
                "type": "router",
                "transports": [
                    {
                        "type": "web",
                        "endpoint": {
                            "type": "tcp",
                            "port": 8080
                        },
                        "paths": {
                            "config": {
                                "type": "json",
                                "value": {
                                    "nodes": [
                                        "ws://localhost:8081/ws",
                                        "ws://localhost:8082/ws",
                                        "ws://localhost:8083/ws",
                                        "ws://localhost:8084/ws"
                                    ]
                                }
                            }
                        }
                    }
                ]
            }
        ]
    }