Javascript implementation of message queue with various delivery strategy.
- Round-robin and broadcast message delivery strategy.
- Various usages:
- Into same node.js process.
- Inter process.
- Inter platform (node.js and browser).
npm install node-queue-lib
var Queue = require('node-queue-lib/queue.core');
var queue = new Queue('Queue name', 'broadcast');
// subscribe on 'Queue name' messages
queue.subscribe(function (err, subscriber) {
subscriber.on('error', function(err){
//
});
subscriber.on('data', function (data, accept) {
console.log(data);
accept(); // accept process message
});
});
// publish message
queue.publish('test');
server message queue:
var SocketIoBridgeServer = require('node-queue-lib/lib/net/socket-io-bridge-server');
var QueueServer = require('node-queue-lib/lib/core/node-queue-server');
var http = require('http');
// TCP port for incoming connections
var port = 80;
// Create Socket.IO transport bridge
var serverBridge = new SocketIoBridgeServer(port, function() {
// return http server instance
return http.createServer();
});
// Create server and start listening
var server = new QueueServer( { serverBridge : serverBridge } );
client: see example
var Queue = require('node-queue-lib');
var url = 'http://localhost';
// create queue instance
var queue = new Queue( url, 'test-queue', 'broadcast' );
// subscribe on 'Queue name' messages
queue.subscribe(function (subscriber) {
subscriber.on('error', function(err){
//
});
subscriber.on('data', function (data, accept) {
console.log(data);
accept(); // accept process message
});
});
// publish message
queue.publish('test');
The server code is the same as above.
client: see example
<!DOCTYPE html>
<html>
<head>
<title>message queue example</title>
<script src="queue.client.js"></script>
<script>
(function(){
var url = 'http://localhost';
// create queue instance
var queue = new queuelib.Queue( url, 'test-queue', 'broadcast' );
// subscribe on 'Queue name' messages
queue.subscribe(function (err, subscriber) {
subscriber.on('error', function(err){
//
});
subscriber.on('data', function (data, accept) {
document.write(data);
accept(); // accept process message
});
});
// publish message
queue.publish('test');
})();
</script>
</head>
<body>
</body>
</html>
npm install node-queue-lib
cd ./node_modules/node-queue-lib/examples
run core test:
node core.js
run client-server test:
node server.js
node client.js
For run browser test locate you browser to http://localhost:8888
For run tests you must have installed grunt.
grunt test
The MIT License
Copyright (c) 2014 Gromozdov Andrey Alexandrovich.
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.