0

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()

2

1 Answer 1

1

According to PubNub's documentation, you should use the following syntax to create a new instance:

var PubNub = require('pubnub');
var pn = new PubNub({
    publishKey : 'demo',
    subscribeKey : 'demo'
});

The error is due to the fact, that you are calling the PubNub constructor as a function:

var pn = pubnub(cfg);

And the error message just confirms that:

TypeError: Cannot call a class as a function at _classCallCheck

Hope this will put you back on track.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.