SocketCluster JavaScript Client

repository·master·Indexed 43 days ago

https://github.com/socketcluster/socketcluster-client

A client-side module for communicating with a SocketCluster server, supporting real-time data transmission, RPCs, and channel-based pub/sub. Version 20.0.2 provides capabilities for transmitting data via socket.transmit, performing Remote Procedure Calls with socket.invoke, and managing channel subscriptions and publishing.

Tokens
2.5K
Snippets
16
Records
16
Agent score
54%

What's inside SocketCluster

  1. Connect over HTTPS

    master

    To connect to a secure server, set secure: true and specify the appropriate port (usually 443). If using self-signed certificates during development, you may need to set wsOptions: { rejectUnauthorized: false }.

    let options = {
      hostname: 'securedomain.com',
      secure: true,
      port: 443,
      wsOptions: { rejectUnauthorized: false }
    };
    let socket = socketClusterClient.create(options);
  2. Embed socketcluster-client in HTML

    master

    You can include the client directly in your HTML page. Once embedded, a global socketClusterClient object becomes available. Note that the src path depends on your HTTP server configuration.

    <script type="text/javascript" src="/socketcluster-client.js"></script>
  3. Subscribe to a channel

    master

    Use socket.subscribe(name) to subscribe to a specific channel. You can await the subscribe event via myChannel.listener('subscribe').once() to ensure the channel state is 'subscribed' before proceeding.

    (async () => {
      // Subscribe to a channel.
      let myChannel = socket.subscribe('myChannel');
    
      await myChannel.listener('subscribe').once();
      // myChannel.state is now 'subscribed'.
    })();
  4. Get a channel without subscribing

    master

    Use socket.channel(name) to obtain a channel object without immediately subscribing to it. You can call myChannel.subscribe() at a later time.

    (async () => {
      let myChannel = socket.channel('myChannel');
    
      // Can subscribe to the channel later as a separate step.
      myChannel.subscribe();
      await myChannel.listener('subscribe').once();
      // myChannel.state is now 'subscribed'.
    })();
  5. Publish data to a channel

    master

    There are several ways to publish data to a channel:

    1. Via Channel object: Use myChannel.transmitPublish(data) (fire-and-forget) or await myChannel.invokePublish(data) (awaits server acknowledgment).
    2. Via Socket object: Use socket.transmitPublish(channelName, data) (fire-and-forget) or await socket.invokePublish(channelName, data) (awaits server acknowledgment).
    // Publish data to the channel.
    myChannel.transmitPublish('This is a message');
    
    // Publish data to the channel from the socket.
    socket.transmitPublish('myChannel', 'This is a message');
    
    (async () => {
      // Publish data to the channel and await for the message to reach the server.
      try {
        await myChannel.invokePublish('This is a message');
      } catch (error) {
        // Handle error.
      }
    
      // Publish data to the channel from the socket and await for the message to reach the server.
      try {
        await socket.invokePublish('myChannel', 'This is a message');
      } catch (error) {
        // Handle error.
      }
    })();
  6. Reference: Connection Options

    master

    The following options are available when calling socketClusterClient.create(options):

    let options = {
      path: '/socketcluster/',
      port: 8000,
      hostname: '127.0.0.1',
      autoConnect: true,
      secure: false,
      connectTimeout: 10000, // milliseconds
      ackTimeout: 10000, // milliseconds
      channelPrefix: null,
      autoReconnectOptions: {
        initialDelay: 10000, // milliseconds
        randomness: 10000, // milliseconds
        multiplier: 1.5, // decimal
        maxDelay: 60000 // milliseconds
      },
      authEngine: null,
      codecEngine: null,
      subscriptionRetryOptions: {},
      wsOptions: { rejectUnauthorized: false },
      query: {
        yourparam: 'hello'
      }
    };