Search code examples
javascriptphppublish-subscribepubnub

Can't subscribe to channel after granting does succeed


This is what I do in PHP:

$pubnub = new Pubnub(array(
  'subscribe_key' => '<SUBSCRIBE_KEY>',
  'publish_key' => '<PUBLISH_KEY>',
  'secret_key' => '<SECRET_KEY>',
  'uuid' => $uuid,
));
$grants = $pubnub->grant(true, true, $channel, $auth_key, 0);

That works. I get a 200 response with my auth key and the correct accesses.

Then I do this in JS:

var pubnub = PUBNUB.init({
  auth_key: settings.auth_key,
  subscribe_key: settings.subscribe_key,
  publish_key: settings.publish_key,
  uuid: settings.uuid,
});

pubnub.subscribe({
  channel: settings.channel,
  // auth_key: settings.auth_key,
  // uuid: settings.uuid,
  presence: function(m) {
    console.log('presence', m);
  },
  message: function(m) {
    console.log('message', m);
  }
});

and that spwans about 10 403 errors per second. I've tried including and excluding a bunch of config vars, like uuid and auth_key, but all I get is very many 403's.

If I include origin: 'pubsub.pubnub.com', the presence event is triggered once or twice, but then still a whole bunch of 403's. If I don't include any origin, only 403's, no events.

Doing a here_now() in JS works fine, but uuids is empty, and occupancy is 0:

setInterval(function() {
  pubnub.here_now({channel: settings.channel}, function(rsp) {
    console.log('here_now', rsp);
  });
}, 2000);

Why does the PHP grant work, but not the JS subscribe?


Solution

  • PubNub Presence and Access Manager

    Anytime you are using Presence together with Access Manager (included free with all accounts) in PubNub, when you grant permissions to channels, you also need to grant access to those channels' -pnpres counterparts if the clients are going to monitor presence (implement the presence callback or enable presence on a channel - depends on the SDK how this is handled).

    $grants = $pubnub->grant(true, true, 'sports,sports-pnpres', $auth_key, 0);
    

    This code example shows how you can grant to multiple channels in one call.