just want understand why pubnub javascript code not receive message channel. can subscribe , publish, if browser sends new publish message, other browser can not receive message. heres code:
$(document).ready(function () { var pubnub = pubnub.init({ subscribe_key: 'subscribe-key-here', publish_key: 'publish-key-here' }); pubnub.subscribe({ channel : "my-channel", message : function(m){ console.log(m) }, callback : function (message) { console.log("callback: ", message)}, connect : function() { console.log("connected") pubnub.publish({ channel: 'my_channel', message: { "color" : "blue" }, callback : function(details) { console.log(details) } }); }, disconnect : function() { console.log("disconnected") }, reconnect : function() { console.log("reconnected") }, error : function() { console.log("network error") }, restore : true }) });
by way code running/testing on nodejs localhost server , in chrome , firefox browser.
code bug - have typo in channel name:
subscribe
usesmy_channel
publish
usesmy-channel
also, using 2 parameters mean same thing in subscribe: message
alias callback
and publish, success
alias (in javascript/node v3.7.20+) callback
, recommended (just because makes more sense).
i have removed callback
parameter subscribe
, replace callback
success
in code below.
corrected code:
$(document).ready(function () { var pubnub = pubnub.init({ subscribe_key: 'subscribe-key-here', publish_key: 'publish-key-here' }); pubnub.subscribe({ channel : "my_channel", message : function (message) { console.log("callback: ", message)}, connect : function() { console.log("connected") pubnub.publish({ channel: 'my_channel', message: { "color" : "blue" }, success : function(details) { console.log(details) } }); }, disconnect : function() { console.log("disconnected") }, reconnect : function() { console.log("reconnected") }, error : function() { console.log("network error") }, restore : true }) });
Comments
Post a Comment