I've written a simple chat using Twilio IP messaging example as a starting point. The idea is to have private channels between various clients and admin. Each client will have a separate private channel with admin. Admin can select which channel to open/subscribe to chat with a particular client.
The issue I'm facing now is I cannot unsubscribe from the channel I previously subscribed.
Here is the link to login as admin, client (test1) and client (test2): http://test.verbery.com/
Steps to reproduce the issue:
Technical details: To subscribe to a channel I used an event "onMessageAdded" to listen to the incoming messages for this channel:
// Listen for new messages sent to the channel
personalChannel.on('messageAdded', function(message) {
printMessage(message.author, message.body);
});
To unsubscribe from the messages I've tried unbind('onMessageAdded')
and off('onMessageAdded')
but it doesn't work, js console says: unbind (or off) is not a function.
Any ideas how to unsubscribe from channel?
If you don't want to leave or unsubscribe channel, you can do this for remove all event in the channel:
activeChannel.removeAllListeners();
I do this, when i switch among channels.
Twilio developer evangelist here.
Bob Sponge is right, you need to call leave()
on the channel in order to properly leave it.
personalChannel.leave();
If you are looking to stay connected to the channel, but stop listening from incoming events, you can unbind your listener. You'd actually do that using removeListener
rather than off
or unbind
. This follows the Node.js EventEmitter API.
personalChannel.removeListener("messageAdded");
Let me know if that helps at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With