At the most basic level,
messages are sent to
channels and then dispatched to
handlers. There are a wide variety of handler types that can easily be extended to provide rich behavior. The foundation handler types include: transforms, filters, routers, splitters and aggregators. Adapters and gateways provide ways in to and out of the messaging system. Channels dispatch messages to one or many handlers using a load balancer or pub-sub respectively, or queue messages until a poller consumes them.
Adapters are provided to aid integrating with popular external systems, protocols and APIs including: Node streams, Redis pub-sub, web sockets and web workers. Expect even more adapters in the future, or contribute your own.
All channels and handlers exist within the context of a message bus. The bus provides factories to create channels and handlers, in addition to a scope for referencing these components by name.
var bus = require'msgs'bus;buschannel'lowercase';bustransformer return messagetoUpperCase; input: 'lowercase' output: 'uppercase' ;buschannel'uppercase';buson'uppercase'console.logstr;;bussend'lowercase' 'hello world'; // 'HELLO WORLD'
This example defines two channels,
uppercase, and a transform that listens for messages on the
lowercase channel converts them to upper case and sends the transformed message to the
uppercase channel. Finally, an adapter listens for messages on the
uppercase channel and logs it to the console. So when we send 'hello world' to the
lowercase channel, 'HELLO WORLD' is logged to the console.
While converting a string to upper case is a bit contrived, it demonstrates the core concepts. A slightly more complex example starts to show the real power.
var bus webSocketServer;require'msgs/adapters/stream';require'msgs/channels/pubsub';bus = require'msgs'bus;webSocketServer = ;buspubsubChannel'broadcast';webSocketServeron'connection'busstreamGatewayconnection output: 'broadcast' input: 'broadcast' ;;
Here we're using a publish-subscribe channel to broadcast all messages received from a web socket to every connected web socket. The
broadcast channel serves as a medium to receive and dispatch messages. For each new web socket connection that is established, the streamGateway reads messages sent to the server, and then writes messages back to the client.
This works as long as there is only ever a single application instance, but what if we need to scale horizontally? In that case, we just need to fold in a inter-process messaging solution, Redis in this case.
var bus webSocketServer redis;require'msgs/adapters/stream';require'msgs/adapters/redis';require'msgs/channels/pubsub';bus = require'msgs'bus;redis = require'redis';webSocketServer = ;buspubsubChannel'fromClient';buspubsubChannel'toClient';webSocketServeron'connection'busstreamGatewayconnection output: 'fromClient' input: 'toClient' ;;busredisGatewayrediscreateClient 'redisTopic' output: 'toClient' input: 'fromClient' ;
We took the previous example, altering the streamGateway to use different channels for sending and receiving messages. The redisGateway bridges these channels while broadcasting messages to every other instance connected to Redis.
Once your application is using messaging, it's rather trivial to extend it into new environments.
If your preferred environment is not supported, please let us know. Some features may not be available in all environments.
Specific browser test are provided by Travis CI and Sauce Labs' Open Sauce Plan. You can see specific browser test results, although odds are they do not reference this specific release/branch/commit.
To install without source:
$ npm install msgs
$ bower install msgs
$ npm install
An ECMAScript 5 compatible environment is assumed. Older browsers, ::cough:: IE, that do not support ES5 natively can be shimmed. Any shim should work, although we've tested against cujo's poly
Before running the test suite for the first time:
$ npm install
To run the suite in node:
$ npm test
To run the suite in a browser:
$ npm start browse to http://localhost:8282/ in the browser(s) you wish to test. It can take a few seconds to start.
You can find us on the cujojs mailing list, or the #cujojs IRC channel on freenode.
Please report issues on GitHub. Include a brief description of the error, information about the runtime (including shims) and any error messages.
Feature requests are also welcome.
Please see CONTRIBUTING.md for details on how to contribute to this project.
Copyright 2012-2014 the original author or authors
msgs.js is made available under the MIT license. See LICENSE.txt for details.
forwardmessages from one channel to another