• Stars
    star
    293
  • Rank 141,748 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript client for SocketCluster

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,
  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,
  rejectUnauthorized: false,
  connectTimeout: 10000, //milliseconds
  ackTimeout: 10000, //milliseconds
  channelPrefix: null,
  disconnectOnUnload: true,
  autoReconnectOptions: {
    initialDelay: 10000, //milliseconds
    randomness: 10000, //milliseconds
    multiplier: 1.5, //decimal
    maxDelay: 60000 //milliseconds
  },
  authEngine: null,
  codecEngine: null,
  subscriptionRetryOptions: {},
  query: {
    yourparam: 'hello'
  }
};

Running the tests

  • Clone this repo: git clone [email protected]: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.

More Repositories

1

socketcluster

Highly scalable realtime pub/sub and RPC framework
JavaScript
6,151
star
2

sc-crud-sample

Sample real-time CRUD inventory tracking app built with SocketCluster
JavaScript
324
star
3

loadbalancer

A sticky load balancer optimized for realtime apps
JavaScript
310
star
4

socketcluster-server

Minimal server module for SocketCluster
JavaScript
108
star
5

ndata

A deep key-value store for Node.js (server and client pair)
JavaScript
78
star
6

asyngular

Highly scalable realtime framework compatible with SocketCluster
JavaScript
77
star
7

client-drivers

List of SocketCluster clients
45
star
8

sc-redis

Redis adapter for SocketCluster
JavaScript
45
star
9

plugins

List of SocketCluster http://socketcluster.io plugins - Pull requests welcome.
33
star
10

sc-crud-rethink

Realtime CRUD data management layer/plugin for SocketCluster using RethinkDB as the database
JavaScript
33
star
11

sc-codec-min-bin

Minimal binary codec for SocketCluster based on MessagePack
JavaScript
24
star
12

socketcluster-client-ios

Native iOS client for SocketCluster
HTML
23
star
13

writable-consumable-stream

An async stream which can be iterated over using a for-await-of loop.
JavaScript
21
star
14

sc-stateless-presence

Plugin for checking user presence in SocketCluster. Optimized for front end use.
JavaScript
20
star
15

asyngular-server

Minimal server module for Asyngular
JavaScript
19
star
16

stream-demux

An alternative to using event listeners; improves control flow whilst helping to avoid memory leaks.
JavaScript
18
star
17

baasil-cli

A CLI tool for creating and deploying cloud native apps to Rancher + Kubernetes infrastructure
JavaScript
18
star
18

scc-state

Cluster state tracking and notification engine for SocketCluster clusters.
JavaScript
16
star
19

socketcluster-website

The SocketCluster website
JavaScript
14
star
20

sc-broker

Default broker client-server pair for SocketCluster
JavaScript
13
star
21

asyngular-client

JavaScript client for Asyngular
JavaScript
13
star
22

scc-broker

Server for the SC cluster - For horizontal scalability.
JavaScript
11
star
23

sc-rabbitmq

RabbitMQ adapter for SocketCluster
JavaScript
9
star
24

socketcluster-client-android

Native Android client for SocketCluster
HTML
8
star
25

iocluster

Realtime clustering engine for SocketCluster
JavaScript
7
star
26

async-stream-emitter

EventEmitter using async iterable streams
JavaScript
7
star
27

sc-auth

Auth module for SocketCluster
JavaScript
7
star
28

consumable-stream

Readable async iterable stream.
JavaScript
6
star
29

fleximap

A flexible hash map which supports working with deep, multidimensional keys.
JavaScript
5
star
30

eetase

EventEmitter to AsyncStreamEmitter converter.
JavaScript
4
star
31

sc-channel

Channels for SocketCluster
JavaScript
4
star
32

skeleton-rendezvous

Node.js module for performing fast rendezvous (HRW) hashing with skeleton - Can efficiently handle a large number of machines/sites.
JavaScript
4
star
33

socketcluster-client-edge

JavaScript
4
star
34

sc-stress-tests

Stress test client tool for SocketCluster
JavaScript
4
star
35

agc-broker

Server for the Asyngular cluster - For horizontal scalability.
JavaScript
2
star
36

sc-field

Field model component for Polymer and SocketCluster
HTML
2
star
37

made-with-sc

A list of projects using SC
2
star
38

sc-simple-broker

Simple broker engine for socketcluster-server
JavaScript
2
star
39

sc-broker-cluster

Broker cluster engine for SocketCluster
JavaScript
2
star
40

sc-errors

Error types for SocketCluster
JavaScript
2
star
41

ncom

A TCP socket pair which trigger a 'message' event only when the full message has been received.
JavaScript
2
star
42

sc-emitter

Emitter implementation (based on component-emitter) with support for Node.js error domains
JavaScript
2
star
43

sc-formatter

Module for serializing and unserializing SocketCluster messages
JavaScript
2
star
44

scc-broker-client

Client for SCC - For horizontal scalability.
JavaScript
2
star
45

sc-collection

SocketCluster collection component for the front end
JavaScript
2
star
46

sc-hot-reboot

Automatically reboot workers when code changes.
JavaScript
2
star
47

agc-state

Cluster state tracking and notification engine for Asyngular clusters.
JavaScript
1
star
48

ag-crud-sample

Sample real-time CRUD inventory tracking app built with Asyngular
JavaScript
1
star
49

sc-model

SocketCluster model component for the front end
JavaScript
1
star
50

sc-framework-health-check

Health check plugin for SocketCluster
JavaScript
1
star
51

scc-semver-report

This micromodule tries to find and report any incompatible components on all servers in your SocketCluster Cluster
JavaScript
1
star
52

ag-request

Asyngular request object
JavaScript
1
star
53

scc-integration-tests

Integration tests for SCC
JavaScript
1
star
54

expirymanager

An object for efficiently managing key expiries for in-memory databases
JavaScript
1
star
55

sc-cluster-state-client

Client for sc-cluster-state-server
1
star
56

ag-collection

SocketCluster collection component for the front end
JavaScript
1
star
57

ag-simple-broker

Simple broker engine for Asyngular
JavaScript
1
star