admin管理员组文章数量:1355657
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
2 Answers
Reset to default 7Two things you should definitely take a look at and update your question appropriately:
- The Pusher Debug Console - is the event shown twice in there?
- 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.
本文标签: javascriptPusher event is triggered twiceStack Overflow
版权声明:本文标题:javascript - Pusher event is triggered twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744053885a2582916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论