I am building a simple stream to publish on PubNub and later consume. I can successfully utilize separate functions which log the output to the console or even create a .json file using the .pipe(new function()). However for some reason with this function I am getting the Cannot call a class function. I have not changed the syntax of the function call at the end of the code, so I am assuming it is something in the PubNubStreamOut() function itself.
TypeError: Cannot call a class as a function at _classCallCheck (D:\pasco\node_modules\pubnub\lib\node\index.js:27:99) at _class (D:\pasco\node_modules\pubnub\lib\node\index.js:37:5) at new PubNubOutStream (D:\pasco\pubnub.js:24:13) at Object. (D:\pasco\pubnub.js:54:42) at Module._compile (module.js:635:30) at Object.Module._extensions..js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10)
The .js file follows:
var pubnub = require('pubnub');
var util = require('util');
var Readable = require('stream').Readable;
var Writable = require('stream').Writable;
var Twitter = require('twitter');
var pncfg = {
ssl : true, // enable TLS Tunneling over TCP
publish_key : "PUB_KEY",
subscribe_key : "SUB_KEY"
};
var twcfg = {
consumer_key:"...",
consumer_secret:"...",
access_token_key:"...",
access_token_secret:"..."
}
function PubNubOutStream(cfg, channel) {
Writable.call(this,{objectMode:true});
var pn = pubnub(cfg);
this._write = function(obj, encoding, callback) {
pn.publish({
channel: channel,
message: obj,
callback: () => callback()
});
};
}
util.inherits(PubNubOutStream, Writable);
function TwitterStream(cfg, query) {
Readable.call(this,{objectMode:true});
var client = new Twitter(cfg);
this._read = function() { /* do nothing */ };
var self = this;
function connect() {
client.stream('statuses/filter', {track: query},
function(stream) {
stream.on('data', (tweet) => self.push(tweet));
stream.on('error', (error) => connect());
});
}
connect();
}
util.inherits(TwitterStream, Readable);
new TwitterStream(twcfg,"#twitter").pipe(new PubNubOutStream(pncfg,"awesome-tweets"));
I am not sure why I am getting the class as a function error since I am using the new PubNubOutStream()
PubNubOutStreamas a class instead of as a function? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Writable.call(this,{objectMode:true});