Code Monkey home page Code Monkey logo

sockjs-rooms's Introduction

sockjs-rooms - small pub/sub system with sockjs

NPM

sockjs-rooms is a libray on top of SOCKJS that allow you create channels (rooms) over a single over a SockJS connection the concept of room applies due to the fact that all clients registered in some channel are able to get the message sent to this channel. This creates a simple pub/sub where clients are able to subscribe some channel and once anyone send a message to a channel the server broadcast this message to all client's registered on room (channel).

This libray an this protocol was created on top of this libray websocket-multiplex

To learn more about the problem of multiplexing channels in a single connection please read more here

Table of contents

Server


Simple server sample

var sockjs = require('sockjs');
var RoomServer = require('sockjs-rooms');

var sockjs_opts = {
  sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"
};
//create service with sockjs
var service = sockjs.createServer(sockjs_opts);
//suply sockjs service to room server
var server = new RoomServer(service);

//register channel on server
var red = server.registerChannel('red');
red.on('connection', function(conn) {
  conn.write('Red is connected');
  conn.on('data', function(data) {
    conn.write(data);
  });
});

//create new channel only for reply message
var latency = server.registerChannel('latency');
latency.on('connection', function(conn) {
  conn.on('data', function(data) {
    conn.write(data);
  });
});

This example we connect register 4 channel, red, bob, carl and on channel for mesure latency You can find full example under the folder example.

Node Client


var multichannelClient = require('sockjs-rooms').client;
//create sockjs-room client with server address
var multiClient = new multichannelClient("http://localhost:9999/multiplex");

//register client on channel latency
var latency = multiClient.channel("latency");

//listening message event for channel latency
latency.on("message",function(message){
  var startDate = +new Date();
  var backMessage = JSON.parse(message.data);
  if(backMessage){
    var endDate = +new Date();
    var diff = endDate - startDate;
    console.log("latency is ",diff,'ms');
  }
});

setInterval(function(){
  var start = +new Date();
  var message = { startTime : start }
  //sent message for client latency
  latency.send(JSON.stringify(message));
},500);

//register other channel on this client
var red = multiClient.channel("red");
red.on('open',function(){});
red.on('close',function(){});
red.on('message',function(message){
  console.log('data from channel red ',message)
});

Browser Client


On the client side (browser) load library like that:

<script src="http://cdn.sockjs.org/websocket-multiplex-0.1.js">
  </script>

Alternatively, if you're using SSL:

<script src="https://d1fxtkz8shb9d2.cloudfront.net/websocket-multiplex-0.1.js">
  </script>

There is a full sample of .html client using this libray under the folder example, check the full example of this. This libray have the source of websocket-multiplex under the folder public.

Protocol


The underlying protocol is quite simple. Each message is a string consisting of three comma separated parts: type, topic and payload. There are three valid message types:

  • sub - expresses a will to subscribe to a given topic.
  • msg - a message with payload is being sent on a topic.
  • uns - a will to unsubscribe from a topic.

Invalid messages like wrong unsubscriptions or publishes to a topic to which a client was not subscribed to are simply ignored.

This protocol assumes that both parties are generally willing to cooperate and that no party makes errors. All invalid messages should be ignored.

It's important to notice that the namespace is shared between both parties. It is not a good idea to use the same topic names on the client and on the server side because both parties may unsubscribe the other from a topic.

sockjs-rooms's People

Contributors

masterviana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

sockjs-rooms's Issues

TypeError: RoomServer is not a constructor

node rooms.js
/home/geo/AEP/webpack/aep/server/nodejs/rooms.js:10
var server = new RoomServer(service)
^

TypeError: RoomServer is not a constructor
at Object. (/home/geo/AEP/webpack/aep/server/nodejs/rooms.js:10:14)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)
geo@geo-System-Product-Name:~/AEP/webpack/aep/server/nodejs$

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.