简体   繁体   中英

Sending Pusher events on private channels

I'm doing some tests on Pusher and I've created an authentication method for private channels following the docs and the auth is working like expected.

(docs: https://pusher.com/docs/authenticating_users#implementing_endpoints )

This is the script that I'm using for subscribing into channels using JSONP:

<script>
  Pusher.logToConsole = true;
  var pusher = new Pusher('<%= ENV['PUSHER_KEY'] %>', {
    cluster: 'us2',
    encrypted: true,
    authTransport: 'jsonp',
    authEndpoint: '/pusher_jsonp_auth',
  });

  var channel = pusher.subscribe("private-channel_test");
  channel.bind('pusher:subscription_succeeded', function() {
    console.log("PVT channel");
  });
  channel.bind('greet', function(data) {
    console.log(data);
  });
</script>

The channel was correctly subscribed, OK! The messages I've got on browser's console:

Pusher : State changed : connecting -> connected with new socket ID 163.232688
pusher.min.js:8 Pusher : Event sent : {"event":"pusher:subscribe","data":{"auth":"1e38f997c10796c55093:4e9e160585dc7c49da68da98e281860c1461ddf7c1b37688aa2fde581b482093","channel":"private-channel_test"}}
pusher.min.js:8 Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"private-channel_test"}
home:35 PVT channel

On the backend I've triggered an event to that channel using the socket_id: Using the ruby pusher gem, I've sent messages like this:

Pusher.trigger('private-channel_test', 'greet', {greet: 'hello world'}, {socket_id: "163.232688"})

The events are sent OK to Pusher, as seen on Debug Console:

Pusher调试控制台

But the events are not received on the frontend, and the bind function never runs when using socket_id and Pusher treats these events as API Messages

Anybody knows what am I doing wrong?

Is this code related or is my understanding of so called Private Channels is wrong?

It looks like you're excluding your subscriber from the broadcast as described here .

Try removing the fourth argument from your trigger call:

Pusher.trigger('private-channel_test', 'greet', {greet: 'hello world'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM