• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated 7 months ago

Reviews

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

Repository Details

Minimal server module for SocketCluster

SocketCluster server

Minimal server module for SocketCluster.

This is a stand-alone server module for SocketCluster. SocketCluster's protocol is backwards compatible with the SocketCluster protocol.

Setting up

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

To install this module:

npm install socketcluster-server

Usage

You need to attach it to an existing Node.js http or https server (example):

const http = require('http');
const socketClusterServer = require('socketcluster-server');

let httpServer = http.createServer();
let agServer = socketClusterServer.attach(httpServer);

(async () => {
  // Handle new inbound sockets.
  for await (let {socket} of agServer.listener('connection')) {

    (async () => {
      // Set up a loop to handle and respond to RPCs for a procedure.
      for await (let req of socket.procedure('customProc')) {
        if (req.data.bad) {
          let error = new Error('Server failed to execute the procedure');
          error.name = 'BadCustomError';
          req.error(error);
        } else {
          req.end('Success');
        }
      }
    })();

    (async () => {
      // Set up a loop to handle remote transmitted events.
      for await (let data of socket.receiver('customRemoteEvent')) {
        // ...
      }
    })();

  }
})();

httpServer.listen(8000);

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

SocketCluster can work without the for-await-of loop; a while loop with await statements can be used instead. See https://github.com/SocketCluster/stream-demux#usage

Compatibility mode

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

let agServer = socketClusterServer.attach(httpServer, {
  protocolVersion: 1,
  path: '/socketcluster/'
});

Running the tests

  • Clone this repo: git clone [email protected]:SocketCluster/socketcluster-server.git
  • Navigate to project directory: cd socketcluster-server
  • Install all dependencies: npm install
  • Run the tests: npm test

Benefits of async Iterable over EventEmitter

  • More readable: Code is written sequentially from top to bottom. It avoids event handler callback hell. It's also much easier to write and read complex integration test scenarios.
  • More succinct: Event streams can be easily chained, filtered and combined using a declarative syntax (e.g. using async generators).
  • More manageable: No need to remember to unbind listeners with removeListener(...); just break out of the for-await-of loop to stop consuming. This also encourages a more declarative style of coding which reduces the likelihood of memory leaks and unintended side effects.
  • Less error-prone: Each event/RPC/message can be processed sequentially in the same order that they were sent without missing any data; even if asynchronous calls are made inside middleware or listeners. On the other hand, with EventEmitter, the listener function for the same event cannot be prevented from running multiple times in parallel; also, asynchronous calls within middleware and listeners can affect the final order of actions; all this can cause unintended side effects.

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-client

JavaScript client for SocketCluster
JavaScript
293
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