@stomp/stompjs vs sockjs-client vs stompjs
WebSocket and STOMP Protocol Libraries
@stomp/stompjssockjs-clientstompjsSimilar Packages:

WebSocket and STOMP Protocol Libraries

WebSocket and STOMP Protocol Libraries are essential tools for enabling real-time communication between clients and servers in web applications. WebSockets provide a full-duplex communication channel over a single TCP connection, allowing for low-latency data exchange. STOMP (Simple Text Oriented Messaging Protocol) is a messaging protocol that runs on top of WebSockets, providing a simple and text-based way to send messages between clients and servers. These libraries implement the WebSocket and STOMP protocols, allowing developers to build interactive applications such as chat systems, live notifications, and collaborative tools. They handle the complexities of establishing connections, sending and receiving messages, and managing topics and queues, making it easier for developers to integrate real-time features into their applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
@stomp/stompjs0881473 kB263 months agoApache-2.0
sockjs-client08,520700 kB30-MIT
stompjs01,448-8812 years agoApache-2.0

Feature Comparison: @stomp/stompjs vs sockjs-client vs stompjs

Protocol Support

  • @stomp/stompjs:

    @stomp/stompjs supports STOMP over WebSocket and provides a flexible architecture that allows for easy integration with different transport protocols. It is designed to work seamlessly with both standard WebSocket connections and fallback transports like SockJS, making it versatile for various environments.

  • sockjs-client:

    sockjs-client is primarily focused on providing a reliable WebSocket-like API with fallback mechanisms for environments where WebSocket connections may fail. It does not implement the STOMP protocol itself but can be used in conjunction with STOMP clients to provide a more robust connection layer.

  • stompjs:

    stompjs supports STOMP over WebSocket and SockJS, providing a simple interface for sending and receiving messages. It is a lightweight implementation that adheres to the STOMP protocol specifications, making it compatible with any STOMP-compliant server.

Advanced Features

  • @stomp/stompjs:

    @stomp/stompjs offers a rich set of features, including support for message acknowledgments, transactions, and headers. It also provides a more modern and flexible API compared to older STOMP clients, making it easier to work with asynchronous messaging patterns.

  • sockjs-client:

    sockjs-client focuses on providing reliable connections with automatic fallback to various transport methods (e.g., XHR, iframe) when WebSockets are not available. It does not provide any STOMP-specific features, as it is primarily a transport layer library.

  • stompjs:

    stompjs provides basic STOMP features such as message sending, subscribing to topics, and handling incoming messages. However, it lacks some of the more advanced features found in newer libraries, such as built-in support for message acknowledgments and transactions.

Bundle Size

  • @stomp/stompjs:

    @stomp/stompjs is relatively lightweight compared to other feature-rich STOMP clients, making it a good choice for performance-sensitive applications. Its modular design allows developers to include only the parts of the library they need, further reducing the overall bundle size.

  • sockjs-client:

    sockjs-client is also lightweight, especially considering the functionality it provides. The library is designed to be efficient and has a small footprint, making it suitable for applications where load time and bandwidth usage are concerns.

  • stompjs:

    stompjs is a minimalistic library with a small bundle size, making it ideal for projects that require basic STOMP functionality without adding significant overhead. Its simplicity and focus on core STOMP features make it a good choice for lightweight applications.

Ease of Use: Code Examples

  • @stomp/stompjs:

    STOMP over WebSocket Example with @stomp/stompjs

    import { Client } from '@stomp/stompjs';
    
    const client = new Client({
      brokerURL: 'ws://localhost:8080/stomp',
      onConnect: () => {
        console.log('Connected');
        client.subscribe('/topic/messages', (message) => {
          console.log('Received:', message.body);
        });
        client.publish({ destination: '/topic/messages', body: 'Hello, STOMP!' });
      },
    });
    
    client.activate();
    
  • sockjs-client:

    WebSocket Fallback Example with sockjs-client

    import SockJS from 'sockjs-client';
    
    const socket = new SockJS('http://localhost:8080/sockjs');
    
    socket.onopen = () => {
      console.log('Connection opened');
      socket.send('Hello, SockJS!');
    };
    
    socket.onmessage = (event) => {
      console.log('Message received:', event.data);
    };
    
    socket.onclose = () => {
      console.log('Connection closed');
    };
    
  • stompjs:

    Basic STOMP Example with stompjs

    import Stomp from 'stompjs';
    
    const socket = new WebSocket('ws://localhost:8080/stomp');
    const client = Stomp.over(socket);
    
    client.connect({}, (frame) => {
      console.log('Connected:', frame);
      client.subscribe('/topic/messages', (message) => {
        console.log('Received:', message.body);
      });
      client.send('/topic/messages', {}, 'Hello, STOMP!');
    });
    

How to Choose: @stomp/stompjs vs sockjs-client vs stompjs

  • @stomp/stompjs:

    Choose @stomp/stompjs if you need a modern, feature-rich STOMP client that supports WebSocket, SockJS, and other transport protocols. It is ideal for applications that require advanced features like message acknowledgments, transactions, and support for multiple WebSocket connections.

  • sockjs-client:

    Choose sockjs-client if you need a library that provides a WebSocket-like API with fallback options for environments where WebSockets are not supported. It is particularly useful for applications that require reliable communication in diverse network conditions and need to handle connection failures gracefully.

  • stompjs:

    Choose stompjs if you are looking for a lightweight STOMP client that works well with WebSocket and SockJS. It is suitable for projects that need basic STOMP functionality without the overhead of a larger library. However, it may lack some advanced features found in more modern clients.

README for @stomp/stompjs

STOMP.js

Build Status - Firefox, Chrome Build Status - Safari, Edge Node.js Tests API Docs Refresh

STOMP.js is a fully-fledged STOMP over WebSocket library for browsers and Node.js, providing seamless integration with STOMP protocol-compliant messaging brokers.

Table of Contents

Introduction

This library enables clients to connect to STOMP brokers over WebSocket (or TCP). It fully implements the STOMP protocol specifications (v1.0, v1.1, and v1.2), making it compatible with any broker that supports STOMP or STOMP over WebSocket.

Popular brokers like RabbitMQ, ActiveMQ, and others provide support for STOMP and STOMP over WebSockets out-of-the-box.

Features

  • Simple and intuitive API for interacting with the STOMP protocol
  • Support for STOMP protocol versions: 1.2, 1.1, and 1.0
  • Support for fallback options when WebSocket is unavailable
  • Supports both browser and Node.js environments
  • Option to connect using STOMP over TCP
  • Full support for binary payloads
  • Compatible with RxJS for reactive programming

Getting Started

This section provides a quick guide to integrating STOMP.js into your browser or Node.js application.

Browser

To use STOMP.js in a browser:

  1. Add the following in your HTML file:

    <script type="importmap">
      {
        "imports": {
          "@stomp/stompjs": "https://ga.jspm.io/npm:@stomp/stompjs@7.0.0/esm6/index.js"
        }
      }
    </script>
    <script
      async
      src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js"
      crossorigin="anonymous"
    ></script>
    
  2. Use the library:

    import { Client } from '@stomp/stompjs';
    
    const client = new Client({
      brokerURL: 'ws://localhost:15674/ws',
      onConnect: () => {
        client.subscribe('/topic/test01', message =>
          console.log(`Received: ${message.body}`)
        );
        client.publish({ destination: '/topic/test01', body: 'First Message' });
      },
    });
    
    client.activate();
    

Node.js

To use STOMP.js in a Node.js environment:

  1. Install the package:

    npm install @stomp/stompjs ws
    
  2. Use it in your application:

    import { Client } from '@stomp/stompjs';
    
    import { WebSocket } from 'ws';
    Object.assign(global, { WebSocket });
    
    const client = new Client({
      brokerURL: 'ws://localhost:15674/ws',
      onConnect: () => {
        client.subscribe('/topic/test01', message =>
          console.log(`Received: ${message.body}`)
        );
        client.publish({ destination: '/topic/test01', body: 'First Message' });
      },
    });
    
    client.activate();
    

Documentation

Comprehensive documentation can be found at: STOMP.js Documentation

Upgrading

If you are updating from an older version of STOMP.js, review the Upgrading Guide for any required changes.

Usage with RxJS

Rx-Stomp builds upon this library, exposing all its features as RxJS Observables, enabling reactive programming patterns.

TypeScript Support

STOMP.js includes built-in TypeScript definitions, eliminating the need for external type definition files. Begin coding with TypeScript out-of-the-box!

Changelog

Visit the Change Log for information about changes, improvements, and fixes in recent releases.

Contributing

Thinking of contributing to STOMP.js? Great! To get started:

  • Read the Contributing Guide for development instructions.
  • Report bugs or suggest features by creating an issue on GitHub.

We welcome contributions from the community!

Authors

This library is made possible by these amazing contributors:

This library is originally based on stompjs by Jeff Mesnil with enhancements and bug fixes from Jeff Lindsay and Vanessa Williams.

License

Licensed under the Apache-2.0 License. See the LICENSE file for details.