Question

I am attempting to listen to a particular event type regardless of the channel it was triggered in. My understanding of the docs (http://pusher.com/docs/client_api_guide/client_events#bind-events/lang=js) was that I can do so by calling the bind method on the pusher instance rather than on a channel instance. Here is my code:

var pusher = new Pusher('MYSECRETAPPKEY', {'encrypted':true}); // Replace with your app key 
var eventName = 'new-comment'; 
var callback = function(data) { 
  // add comment into page 
  console.log(data); 
};

pusher.bind(eventName, callback); 

I then used the Event Creator tool in my account portal to generate an event. I used a random channel name, set the Event to "new-comment" and just put in some random piece of text into the Event Data. But, I am getting nothing appearing in my Console.

I am using https://d3dy5gmtp8yhk7.cloudfront.net/2.1/pusher.min.js, and performing this test in the latest Chrome.

What am I missing?

Thanks!

Shaheeb R.

Was it helpful?

Solution

Pusher will only send events to the client if that client has subscribed to the channel. So, the first thing you need to do is subscribe the channel. Binding to the event on the client:

pusher.bind('event_name', function( data ) {
  // handle update
} );

This is also known as "global event binding".

I've tested this using this code and it does work: http://jsbin.com/AROvEDO/1/edit

For completeness, here's the code:

var pusher = new Pusher('APP_KEY');
var channel = pusher.subscribe('test_channel');
pusher.bind('my_event', function(data) {
  alert(data.message);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top