socketcluster-client
Client side of SocketCluster
Last updated 11 years ago by topcloudsystems .
Original npm · Tarball · package.json
$ npm install socketcluster-client 
SYNC missed versions from official npm registry.

SocketCluster JavaScript client

Client module for SocketCluster.

Setting up

You will need to install both socketcluster-client and socketcluster-server (https://github.com/SocketCluster/socketcluster-server).

To install this module:

npm install socketcluster-client

How to use

The socketcluster-client script is called socketcluster-client.js (located in the main socketcluster-client directory). Embed it in your HTML page like this:

<script type="text/javascript" src="/socketcluster-client.js"></script>

* Note that the src attribute may be different depending on how you setup your HTTP server.

Once you have embedded the client socketcluster-client.js into your page, you will gain access to a global socketClusterClient object. You may also use CommonJS require or ES6 module imports.

Connect to a server

let socket = socketClusterClient.create({
  hostname: 'localhost',
  port: 8000
});

Transmit data

// Transmit some data to the server.
// It does not expect a response from the server.
// From the server socket, it can be handled using either:
// - for await (let data of socket.receiver('foo')) {}
// - let data = await socket.receiver('foo').once()
socket.transmit('foo', 123);

Invoke an RPC

(async () => {

  // Invoke an RPC on the server.
  // It expects a response from the server.
  // From the server socket, it can be handled using either:
  // - for await (let req of socket.procedure('myProc')) {}
  // - let req = await socket.procedure('myProc').once()
  let result = await socket.invoke('myProc', 123);

})();

Subscribe to a channel

(async () => {

  // Subscribe to a channel.
  let myChannel = socket.subscribe('myChannel');

  await myChannel.listener('subscribe').once();
  // myChannel.state is now 'subscribed'.

})();

Get a channel without subscribing

(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'.

})();

Publish data to a channel

// 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.
  }
})();

Consume data from a channel

(async () => {

  for await (let data of myChannel) {
    // ...
  }

})();

Connect over HTTPS:

let options = {
  hostname: 'securedomain.com',
  secure: true,
  port: 443,
  wsOptions: { rejectUnauthorized: false } // Only necessary during debug if using a self-signed certificate
};
// Initiate the connection to the server
let socket = socketClusterClient.create(options);

For more detailed examples of how to use SocketCluster, see test/integration.js. Also, see tests from the socketcluster-server module.

Connect Options

See all available options: https://socketcluster.io/

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'
  }
};

Running the tests

  • Clone this repo: git clone git@github.com:SocketCluster/socketcluster-client.git
  • Navigate to project directory: cd socketcluster-client
  • Install all dependencies: npm install
  • Run the tests: npm test

Compatibility mode

For compatibility with an existing SocketCluster server, set the protocolVersion to 1 and make sure that the path matches your old server path:

let socket = socketClusterClient.create({
  protocolVersion: 1,
  path: '/socketcluster/'
});

Developing

Install all dependencies

cd socketcluster-client

npm install -g gulp gulp-cli browserify uglify-es

npm install

Building

To build the SocketCluster client:

npm run build

Change log

See the 'releases' section for changes: https://github.com/SocketCluster/socketcluster-client/releases

License

(The MIT License)

Copyright (c) 2013-2023 SocketCluster.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 19.2.1                                ...           latest (8 months ago)

234 Versions

  • 19.2.1                                ...           8 months ago
  • 19.2.0                                ...           8 months ago
  • 19.1.2                                ...           9 months ago
  • 19.1.1                                ...           9 months ago
  • 19.1.0                                ...           a year ago
  • 19.0.2                                ...           a year ago
  • 19.0.1                                ...           a year ago
  • 19.0.0                                ...           a year ago
  • 18.3.0                                ...           a year ago
  • 18.2.2                                ...           a year ago
  • 18.2.1                                ...           a year ago
  • 18.2.0                                ...           a year ago
  • 18.1.3                                ...           a year ago
  • 18.1.2                                ...           a year ago
  • 18.1.1                                ...           a year ago
  • 18.1.0                                ...           a year ago
  • 18.0.3                                ...           a year ago
  • 18.0.2                                ...           a year ago
  • 18.0.1                                ...           a year ago
  • 18.0.0                                ...           a year ago
  • 17.2.2                                ...           a year ago
  • 17.2.1                                ...           a year ago
  • 17.2.0                                ...           a year ago
  • 17.1.1                                ...           2 years ago
  • 17.1.0                                ...           2 years ago
  • 17.0.1                                ...           2 years ago
  • 17.0.0                                ...           2 years ago
  • 16.1.1                                ...           3 years ago
  • 16.1.0                                ...           3 years ago
  • 16.0.4                                ...           3 years ago
  • 14.3.2                                ...           4 years ago
  • 16.0.3                                ...           4 years ago
  • 16.0.2                                ...           4 years ago
  • 16.0.1                                ...           5 years ago
  • 16.0.0                                ...           5 years ago
  • 15.1.0                                ...           5 years ago
  • 15.0.3                                ...           5 years ago
  • 15.0.2                                ...           5 years ago
  • 15.0.1                                ...           5 years ago
  • 15.0.0                                ...           5 years ago
  • 14.3.1                                ...           5 years ago
  • 14.3.0                                ...           5 years ago
  • 14.2.2                                ...           6 years ago
  • 14.2.1                                ...           6 years ago
  • 14.2.0                                ...           6 years ago
  • 14.1.0                                ...           6 years ago
  • 14.0.0                                ...           6 years ago
  • 13.0.1                                ...           6 years ago
  • 13.0.0                                ...           7 years ago
  • 12.0.0                                ...           7 years ago
  • 11.2.2                                ...           7 years ago
  • 11.2.1                                ...           7 years ago
  • 11.2.0                                ...           7 years ago
  • 11.1.0                                ...           7 years ago
  • 11.0.2                                ...           7 years ago
  • 11.0.1                                ...           7 years ago
  • 11.0.0                                ...           7 years ago
  • 10.1.2                                ...           7 years ago
  • 10.1.1                                ...           7 years ago
  • 10.1.0                                ...           7 years ago
  • 10.0.1                                ...           7 years ago
  • 10.0.0                                ...           7 years ago
  • 9.0.4                                ...           7 years ago
  • 9.0.3                                ...           7 years ago
  • 9.0.2                                ...           7 years ago
  • 9.0.1                                ...           7 years ago
  • 9.0.0                                ...           7 years ago
  • 8.0.2                                ...           7 years ago
  • 8.0.1                                ...           7 years ago
  • 8.0.0                                ...           7 years ago
  • 7.0.2                                ...           7 years ago
  • 7.0.1                                ...           7 years ago
  • 7.0.0                                ...           7 years ago
  • 6.4.0                                ...           7 years ago
  • 6.3.0                                ...           7 years ago
  • 6.2.2                                ...           7 years ago
  • 6.2.1                                ...           7 years ago
  • 6.2.0                                ...           7 years ago
  • 6.1.1                                ...           7 years ago
  • 6.1.0                                ...           7 years ago
  • 5.5.2                                ...           8 years ago
  • 5.5.1                                ...           8 years ago
  • 5.5.0                                ...           8 years ago
  • 5.4.0                                ...           8 years ago
  • 5.3.1                                ...           8 years ago
  • 5.3.0                                ...           8 years ago
  • 5.2.5                                ...           8 years ago
  • 5.2.4                                ...           8 years ago
  • 5.2.3                                ...           8 years ago
  • 5.2.2                                ...           8 years ago
  • 5.2.1                                ...           8 years ago
  • 5.2.0                                ...           8 years ago
  • 5.1.1                                ...           8 years ago
  • 5.1.0                                ...           8 years ago
  • 5.0.21                                ...           8 years ago
  • 5.0.20                                ...           8 years ago
  • 5.0.19                                ...           8 years ago
  • 5.0.18                                ...           8 years ago
  • 5.0.17                                ...           8 years ago
  • 5.0.16                                ...           8 years ago
  • 5.0.15                                ...           8 years ago
  • 5.0.14                                ...           8 years ago
  • 5.0.13                                ...           8 years ago
  • 5.0.12                                ...           8 years ago
  • 5.0.11                                ...           8 years ago
  • 5.0.10                                ...           8 years ago
  • 5.0.9                                ...           8 years ago
  • 5.0.8                                ...           8 years ago
  • 5.0.7                                ...           8 years ago
  • 5.0.6                                ...           8 years ago
  • 5.0.5                                ...           8 years ago
  • 5.0.4                                ...           8 years ago
  • 5.0.3                                ...           8 years ago
  • 5.0.2                                ...           8 years ago
  • 5.0.1                                ...           8 years ago
  • 5.0.0                                ...           8 years ago
  • 4.3.19                                ...           9 years ago
  • 4.3.18                                ...           9 years ago
  • 4.3.17                                ...           9 years ago
  • 4.3.16                                ...           9 years ago
  • 4.3.15                                ...           9 years ago
  • 4.3.14                                ...           9 years ago
  • 4.3.13                                ...           9 years ago
  • 4.3.12                                ...           9 years ago
  • 4.3.11                                ...           9 years ago
  • 4.3.10                                ...           9 years ago
  • 4.3.9                                ...           9 years ago
  • 4.3.8                                ...           9 years ago
  • 4.3.7                                ...           9 years ago
  • 4.3.6                                ...           9 years ago
  • 4.3.5                                ...           9 years ago
  • 4.3.4                                ...           9 years ago
  • 4.3.3                                ...           9 years ago
  • 4.3.2                                ...           9 years ago
  • 4.3.1                                ...           9 years ago
  • 4.1.6                                ...           9 years ago
  • 4.1.5                                ...           9 years ago
  • 4.1.4                                ...           9 years ago
  • 4.1.3                                ...           9 years ago
  • 4.1.2                                ...           9 years ago
  • 4.1.1                                ...           9 years ago
  • 4.1.0                                ...           9 years ago
  • 4.0.1                                ...           9 years ago
  • 4.0.0                                ...           9 years ago
  • 3.1.1                                ...           9 years ago
  • 3.1.0                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.4.1                                ...           9 years ago
  • 2.4.0                                ...           9 years ago
  • 2.3.19                                ...           9 years ago
  • 2.3.18                                ...           9 years ago
  • 2.3.17                                ...           9 years ago
  • 2.3.16                                ...           9 years ago
  • 2.3.15                                ...           9 years ago
  • 2.3.14                                ...           9 years ago
  • 2.3.13                                ...           9 years ago
  • 2.3.12                                ...           9 years ago
  • 2.3.11                                ...           9 years ago
  • 2.3.10                                ...           9 years ago
  • 2.3.9                                ...           9 years ago
  • 2.3.7                                ...           9 years ago
  • 2.3.3                                ...           9 years ago
  • 2.3.2                                ...           9 years ago
  • 2.3.1                                ...           9 years ago
  • 2.3.0                                ...           9 years ago
  • 2.2.38                                ...           9 years ago
  • 2.2.37                                ...           9 years ago
  • 2.2.36                                ...           9 years ago
  • 2.2.33                                ...           9 years ago
  • 2.2.32                                ...           10 years ago
  • 2.2.31                                ...           10 years ago
  • 2.2.30                                ...           10 years ago
  • 2.2.29                                ...           10 years ago
  • 2.2.28                                ...           10 years ago
  • 2.2.27                                ...           10 years ago
  • 2.2.26                                ...           10 years ago
  • 2.2.25                                ...           10 years ago
  • 2.2.23                                ...           10 years ago
  • 2.2.20                                ...           10 years ago
  • 2.2.11                                ...           10 years ago
  • 2.2.9                                ...           10 years ago
  • 1.3.11                                ...           10 years ago
  • 1.3.8                                ...           10 years ago
  • 1.3.7                                ...           10 years ago
  • 1.3.6                                ...           10 years ago
  • 1.3.5                                ...           10 years ago
  • 1.3.4                                ...           10 years ago
  • 1.3.3                                ...           10 years ago
  • 1.3.0                                ...           10 years ago
  • 1.0.8                                ...           10 years ago
  • 1.0.7                                ...           10 years ago
  • 1.0.6                                ...           10 years ago
  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           10 years ago
  • 1.0.2                                ...           10 years ago
  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           10 years ago
  • 0.9.37                                ...           10 years ago
  • 0.9.36                                ...           10 years ago
  • 0.9.35                                ...           10 years ago
  • 0.9.34                                ...           10 years ago
  • 0.9.33                                ...           10 years ago
  • 0.9.32                                ...           10 years ago
  • 0.9.31                                ...           10 years ago
  • 0.9.30                                ...           10 years ago
  • 0.9.29                                ...           10 years ago
  • 0.9.28                                ...           10 years ago
  • 0.9.27                                ...           10 years ago
  • 0.9.26                                ...           10 years ago
  • 0.9.25                                ...           10 years ago
  • 0.9.24                                ...           10 years ago
  • 0.9.23                                ...           10 years ago
  • 0.9.22                                ...           10 years ago
  • 0.9.21                                ...           10 years ago
  • 0.9.20                                ...           10 years ago
  • 0.9.19                                ...           10 years ago
  • 0.9.18                                ...           11 years ago
  • 0.9.17                                ...           11 years ago
  • 0.9.16                                ...           11 years ago
  • 0.9.15                                ...           11 years ago
  • 0.9.14                                ...           11 years ago
  • 0.9.13                                ...           11 years ago
  • 0.9.12                                ...           11 years ago
  • 0.9.11                                ...           11 years ago
  • 0.9.10                                ...           11 years ago
  • 0.9.9                                ...           11 years ago
  • 0.9.8                                ...           11 years ago
  • 0.9.7                                ...           11 years ago
  • 0.9.6                                ...           11 years ago
  • 0.9.5                                ...           11 years ago
  • 0.9.4                                ...           11 years ago
  • 0.9.3                                ...           11 years ago
  • 0.9.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE