最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pusher event is triggered twice - Stack Overflow

programmeradmin0浏览0评论

I'm using Pusher (pusher) to trigger notification to all logged in clients whenever the admin sends one.

For some reason the event is shooting twice, although I only trigger it once.

Client side subscription code:

var handleToastrListener = function() {

     var pusher = new Pusher("913284db62a0cc237db4");
     var channel = pusher.subscribe('toastr-channel');

           channel.bind('new-toast', function(data) {

            toastr.options = data.options;
            var $toast = toastr[data.scf](data.msg, data.title);

           return true;

           });  
   }

       handleToastrListener();

Server side publish code (PHP using pusher package):

 $pusher = new Pusher(PUSHER_KEY, PUSHER_SECRET, PUSHER_APP_ID);
 $pusher->trigger('toastr-channel', 'new-toast', $input );

The Pusher debug console shows that only one message was received.

The pusher-js Javascript logging, however, shows two messages:

Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"} app.js:143
Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"}

looking further I found the subscription occurs twice, although I call it only once:

Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded app.js:143
Pusher : State changed : connecting -> connected app.js:143
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded 

I'm using Pusher (pusher.) to trigger notification to all logged in clients whenever the admin sends one.

For some reason the event is shooting twice, although I only trigger it once.

Client side subscription code:

var handleToastrListener = function() {

     var pusher = new Pusher("913284db62a0cc237db4");
     var channel = pusher.subscribe('toastr-channel');

           channel.bind('new-toast', function(data) {

            toastr.options = data.options;
            var $toast = toastr[data.scf](data.msg, data.title);

           return true;

           });  
   }

       handleToastrListener();

Server side publish code (PHP using pusher package):

 $pusher = new Pusher(PUSHER_KEY, PUSHER_SECRET, PUSHER_APP_ID);
 $pusher->trigger('toastr-channel', 'new-toast', $input );

The Pusher debug console shows that only one message was received.

The pusher-js Javascript logging, however, shows two messages:

Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"} app.js:143
Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"}

looking further I found the subscription occurs twice, although I call it only once:

Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded app.js:143
Pusher : State changed : connecting -> connected app.js:143
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded 
Share Improve this question edited Feb 19, 2014 at 8:55 Matanya asked Feb 19, 2014 at 8:06 MatanyaMatanya 6,3369 gold badges49 silver badges82 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Two things you should definitely take a look at and update your question appropriately:

  1. The Pusher Debug Console - is the event shown twice in there?
  2. pusher-js JavaScript logging - is the event being logged as ining twice?

The other mon problem here is that sometimes events may have been bound to twice - hence, two callbacks. However, your code doesn't suggest that this is happening.

To solve this, you need to unbind the channel before binding.

pusherClient.subscribe(1234);
pusherClient.bind(1234);
pusherClient.bind("ining-message", (text) => {
  console.log(text);
});

See here for more info.

发布评论

评论列表(0)

  1. 暂无评论