Giter VIP home page Giter VIP logo

socketcluster-server's Introduction

socketcluster-server

Minimal server module for SocketCluster

This is a stand-alone server module for SocketCluster. This module offers the most flexibility when creating a SocketCluster service but requires the most work to setup. The repository for the full-featured framework is here: https://github.com/SocketCluster/socketcluster

Setting up

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

To install this module: npm install socketcluster-server

Using with basic http(s) module (example)

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

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

var httpServer = http.createServer();
var scServer = socketClusterServer.attach(httpServer);

scServer.on('connection', function (socket) {
  // ... Handle new socket connections here
});

httpServer.listen(8000);

Using with Express (example)

var http = require('http');
var socketClusterServer = require('socketcluster-server');
var serveStatic = require('serve-static');
var path = require('path');
var app = require('express')();

app.use(serveStatic(path.resolve(__dirname, 'public')));

var httpServer = http.createServer();

// Attach express to our httpServer
httpServer.on('request', app);

// Attach socketcluster-server to our httpServer
var scServer = socketClusterServer.attach(httpServer);

scServer.on('connection', function (socket) {
  // ... Handle new socket connections here
});

httpServer.listen(8000);

Note that the full SocketCluster framework (https://github.com/SocketCluster/socketcluster) uses this module behind the scenes so the API is exactly the same and it works with the socketcluster-client out of the box. The main difference with using socketcluster-server is that you won't get features like:

  • Automatic scalability across multiple CPU cores.
  • Resilience; you are responsible for respawning the process if it crashes.
  • Convenience; It requires more work up front to get working (not good for beginners).
  • Pub/sub channels won't scale across multiple socketcluster-server processes/hosts by default.*

* Note that the socketClusterServer.attach(httpServer, options); takes an optional options argument which can have a brokerEngine property - By default, socketcluster-server uses sc-simple-broker which is a basic single-process in-memory broker. If you want to add your own brokerEngine (for example to scale your socketcluster-servers across multiple cores/hosts), then you might want to look at how sc-simple-broker was implemented.

The full SocketCluster framework uses a different broker engine: sc-broker-cluster(https://github.com/SocketCluster/sc-broker-cluster) - This is a more complex brokerEngine - It allows messages to be brokered between multiple processes and can be synchronized with remote hosts too so you can get both horizontal and vertical scalability.

The main benefit of this module is that it gives you maximum flexibility. You just need to attach it to a Node.js http server so you can use it alongside pretty much any framework.

socketcluster-server's People

Contributors

jondubois avatar megagm avatar dpawlowski-cksource avatar jansenignacio avatar kherock avatar mirague avatar spaintrain avatar toredash avatar kujon avatar robhogan avatar

Watchers

James Cloos avatar Nhut Phan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.