Event Hub and clients for event-based applications
Last updated 8 years ago by zzo .
Repository · Original npm · Tarball · package.json
$ cnpm install EventHub -g
SYNC missed versions from official npm registry.


EventHub provides a server-side event hub and server and client side libraries to communicate with the hub. The hub broadcasts all events to all connected clients. Event callbacks are supported.


% npm install EventHub -g
% npm start EventHub -g

This will install and start the EventHub on port 5883


EventHub's default port is 5883. Use the npm configuration variable to change it:

% npm config set EventHub.port = 8888

Will change the port the EventHub listens on. Ensure you update your clients to point to this port.

The Hub

The hub itself is hub/eventHub.js. This NodeJS implementation will listen on a given port for connections. You can run this on any host/port, just tell each client where this thing is running and they will connect to it.

The Clients



A YUI3 module called 'EventHub' in clients/browsers/yui3.js provides a client-side module for the EventHub. Look in examples/browser for a test drive. You must first start the eventHub, then set up some HTML like so:

 <script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script>
 <script src="/socket.io/socket.io.js"></script>
 <script src="/clients/browser/yui3.js"></script>
 <button id="button">Press Me</button>
    YUI().use('node', 'EventHub', function(Y) {

        var hub = new Y.EventHub(io, 'http://<HUB HOST>:<HUB PORT>');
        hub.on('eventHubReady', function() {
                function(event) {
                    hub.fire('click', { button: 'clicked' },
                        function(back, b2) { 
                            Y.log("callback from event listener!");
                function(o1, fn) { 
                    Y.log('got local click event: ' + o1.button); 

Note the <HUB HOST>:<HUB PORT> to connect to your running EventHub. This loads in YUI3, then socket.io, and finally the YUI3 EventHub client library (yui3.js). Simply instantiate a new Y.EventHub, and when it's ready you're ready.

This example requires a server to server this HTML the socket.io library, and the client hub code, which is simply:

var connect = require('connect')
    , server = connect.createServer(
        , connect.static(__dirname + '/../..')
        , connect.static(__dirname)
    , io = require('socket.io').listen(server)
    , port = 8888

console.log('Listening on port ' + port);

This uses the Connect framework to serve the static files (the HTML, the socket.io library, and the yui3 event hub module.

For this to all work. Point your browser at this host, port 8888, and the example will load. Finally you will also need a listener for the 'click' event this example fires. That is in server example directory and explained next.


the jQuery client is used very similarly to the YUI3 client:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
 <script src="/socket.io/socket.io.js"></script>
 <script src="/clients/browser/jquery.js"></script>
 <button id="button">Press Me</button>
    var hub = new $.fn.eventHub(io, 'http://<HUB HOST>:<HUB PORT>');
    hub.bind('eventHubReady', function() {
        console.log('EVENT HUB READY!');
            function(event) {
                console.log('clicked on button - making new event for hub');
                hub.trigger('click', { button: 'clicked' },
                    function(back, b2) { 
                        console.log("callback from event listener!");
            function(event, o1, fn) { 
                console.log('got local click event: ' + event); 

Loading up jQuery, socket.io, and the jQuery EventHub client and bind to the button's click event. When the hub is ready you can 'trigger' events on it and 'bind' to events from it. I'm no jQuery expert so I hope this is sane for you jQuery people!


So we can serve the event hub library, the socket.io library, and some example HTML to fire a 'click' event when you click on the button. Finally we need a listener to do something, an example of a server-side event hub client does just that. After the EventHub is started, run examples/nodejs/client.js

% node examples/nodejs/client.js

The client will connect to the specified EventHub and uses the clients/server/eventClient.js NodeJS module to handle all EventHub interaction. It listens for a 'click' event and excercises the callback to return data back to the event emitter.

Putting It All Together

So you start the EventHub, start the example server, and start the example NodeJS client. Go to your browser and point it at the example server and click the button. An event will propagate from the browser, to the EventHub, and finally to the NodeJS client. The NodeJS client will call the event's callback which will execute back in the browser. Simple!

You've not got an awesome event-based archetecture so go wild!

Current Tags

  • 1.2.4                                ...           latest (8 years ago)

13 Versions

  • 1.2.4                                ...           8 years ago
  • 1.2.3                                ...           8 years ago
  • 1.2.2                                ...           8 years ago
  • 1.2.1                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.5                                ...           8 years ago
  • 1.1.4                                ...           9 years ago
  • 1.1.3                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 11
Dependencies (2)
Dev Dependencies (1)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |