UNPKG

@tsjing/nsqjs

Version:
168 lines (143 loc) 4.92 kB
// Generated by CoffeeScript 1.10.0 var ConnectionConfig, Debug, EventEmitter, Writer, WriterNSQDConnection, _, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; Debug = require('debug'); EventEmitter = require('events').EventEmitter; _ = require('underscore'); ConnectionConfig = require('./config').ConnectionConfig; WriterNSQDConnection = require('./nsqdconnection').WriterNSQDConnection; /* Publish messages to nsqds. Usage: w = new Writer '127.0.0.1', 4150 w.connect() w.on Writer.READY, -> * Send a single message w.publish 'sample_topic', 'one' * Send multiple messages w.publish 'sample_topic', ['two', 'three'] w.on Writer.CLOSED, -> console.log 'Writer closed' */ Writer = (function(superClass) { extend(Writer, superClass); Writer.READY = 'ready'; Writer.CLOSED = 'closed'; Writer.ERROR = 'error'; function Writer(nsqdHost, nsqdPort, options) { this.nsqdHost = nsqdHost; this.nsqdPort = nsqdPort; Writer.__super__.constructor.apply(this, arguments); this.setMaxListeners(10000); this.debug = Debug("nsqjs:writer:" + this.nsqdHost + "/" + this.nsqdPort); this.config = new ConnectionConfig(options); this.config.validate(); this.ready = false; this.debug('Configuration'); this.debug(this.config); } Writer.prototype.connect = function() { this.conn = new WriterNSQDConnection(this.nsqdHost, this.nsqdPort, this.config); this.debug('connect'); this.conn.connect(); this.conn.on(WriterNSQDConnection.READY, (function(_this) { return function() { _this.debug('ready'); _this.ready = true; return _this.emit(Writer.READY); }; })(this)); this.conn.on(WriterNSQDConnection.CLOSED, (function(_this) { return function() { _this.debug('closed'); _this.ready = false; return _this.emit(Writer.CLOSED); }; })(this)); this.conn.on(WriterNSQDConnection.ERROR, (function(_this) { return function(err) { _this.debug('error', err); _this.ready = false; return _this.emit(Writer.ERROR, err); }; })(this)); return this.conn.on(WriterNSQDConnection.CONNECTION_ERROR, (function(_this) { return function(err) { _this.debug('error', err); _this.ready = false; return _this.emit(Writer.ERROR, err); }; })(this)); }; /* Publish a message or a list of messages to the connected nsqd. The contents of the messages should either be strings or buffers with the payload encoded. Arguments: topic: A valid nsqd topic. msgs: A string, a buffer, a JSON serializable object, or a list of string / buffers / JSON serializable objects. */ Writer.prototype.publish = function(topic, msgs, callback) { var connState, err, failed, msg, ready, ref, ref1, remove; connState = (ref = this.conn) != null ? (ref1 = ref.statemachine) != null ? ref1.current_state_name : void 0 : void 0; if (!this.conn || (connState === 'CLOSED' || connState === 'ERROR')) { err = new Error('No active Writer connection to send messages'); } if (!msgs || _.isEmpty(msgs)) { err = new Error('Attempting to publish an empty message'); } if (err) { if (callback) { return callback(err); } throw err; } if (!this.ready) { ready = (function(_this) { return function() { remove(); return _this.publish(topic, msgs, callback); }; })(this); failed = function(err) { err || (err = new Error('Connection closed!')); remove(); return callback(err); }; remove = (function(_this) { return function() { _this.removeListener(Writer.READY, ready); _this.removeListener(Writer.ERROR, failed); return _this.removeListener(Writer.CLOSED, failed); }; })(this); this.on(Writer.READY, ready); this.on(Writer.ERROR, failed); this.on(Writer.CLOSED, failed); return; } if (!_.isArray(msgs)) { msgs = [msgs]; } msgs = (function() { var i, len, results; results = []; for (i = 0, len = msgs.length; i < len; i++) { msg = msgs[i]; if (_.isString(msg) || Buffer.isBuffer(msg)) { results.push(msg); } else { results.push(JSON.stringify(msg)); } } return results; })(); return this.conn.produceMessages(topic, msgs, callback); }; Writer.prototype.close = function() { return this.conn.destroy(); }; return Writer; })(EventEmitter); module.exports = Writer;