2

I am updating from pubnub v3 javascript to v4, the publish and subscribe is working, but do not update the database in live... I readed the migration topic here, but I dont understand how, and where I have to integrate the listeners function, I think that is the problem.

var pubnub = new PubNub({
                subscribeKey : 'xxx',
                publishKey : 'zzz',
                ssl: true
            });

pubnub.subscribe({
    channels : ['my_channel'],
    message : function( message, env, channel ){
        var getMessage = JSON.stringify(message);
// I readed that I should remove stringify
// to: var getMessage = message;
            var obj = jQuery.parseJSON(getMessage);
            var data = setInterval(function(){ removeTdBorder(); }, 3000);
... other functions ...
});

and the publish function

function saveToDatabase(editableObj,column,id) {

    if(editableObj.tagName == "TD")
    {
        var editval = editableObj.innerHTML;
    }else{
        var editval = jQuery('[name="'+column+'"]').val();
    }

    pubnub.publish({
        channel: 'my_channel',        
        message:    {
                        "message" : editval,
                        "column"  : column,
                        "id"      : id,
                    },
        callback : function(m){
        }
    });

I have a table component, where I insert data to the cells, with pubnub real time javascript api. I appreciate any help! Thank you in advance!

user348246
  • 380
  • 4
  • 16
  • 1
    I mean, when a user update the data in the cell (this savetodatabase function save the data to database) then the another user who is subscribed to the channel got the message but for her it does not update the data in that cell, if I am right in v3 there was the callback function for this If I am correct. And yes I readed that adding listener part but I cannot understand, where I have to add and what function has... thanks for comment @CraigConover – user348246 May 30 '17 at 08:06
  • 2
    I figured out how to resolve, you are right, I have moved the functions from subscribe to listener, and the v3 have a simple response, compared to v4 `pubnub.addListener({ message : function( message, env, channel ){ var getMessage = JSON.stringify(message.message); var obj = jQuery.parseJSON(getMessage);` that is for v4, in v3 this works `pubnub.subscribe({ message : function( message, env, channel ){ var getMessage = JSON.stringify(message); var obj = jQuery.parseJSON(getMessage);` – user348246 May 30 '17 at 11:57
  • added official answer – Craig Conover Jun 15 '17 at 21:30

1 Answers1

1

PubNub v4 SDK Listeners

There is no db update feature in PubNub. To answer the how to listen to messages question, there is no callback in subscribe anymore. You receive messages in the addListener callbacks.

See this addListener API Ref for details. I think the issue is that the migration guide doesn't have any sample addListener code. Here is the sample code from the docs:

pubnub.addListener({
    message: function(m) {
        // handle message
        var channelName = m.channel; 
        var pubTT = m.timetoken; // Publish timetoken
        var msg = m.message; 
    }
})
Community
  • 1
  • 1
Craig Conover
  • 4,710
  • 34
  • 59