当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Pusher: How to get private channel subscription count in pusher?

问题描述:

What I want: How can i get subscription count after subscription of a private channel in pusher.Actually i want private chat between only 2 users.

What I Know and Doing: . I know that i private channel can be subscribed by multiple clients(users).So whenever server trigger the data to private channel then all subscribed user get that response.

Channel subscription code in java script (Client-1):

<!DOCTYPE html>

<head>

<title>Pusher Test</title>

<script src="https://js.pusher.com/3.2/pusher.min.js"></script>

<script>

// Enable pusher logging - don't include this in production

Pusher.logToConsole = true;

var pusher = new Pusher('PUSHER_KEY', {

authEndpoint: 'http://localhost:8080/medecube/rest/initial/pusher/auth',

auth: {

headers: {

'X-CSRF-Token': "12345"

}

}

});

var channel = pusher.subscribe('private-channel');

channel.bind('pusher:subscription_succeeded', function() {

var triggered = channel.trigger('client-myEvent', { "message": "i am pusher client1" });

});

</script>

</head>

Same channel subscribed by client 2

Channel subscription code in java script (Client-2):

<!DOCTYPE html>

<head>

<title>Pusher Test</title>

<script src="https://js.pusher.com/3.2/pusher.min.js"></script>

<script>

// Enable pusher logging - don't include this in production

Pusher.logToConsole = true;

var pusher = new Pusher('PUSHER_KEY', {

authEndpoint: 'http://localhost:8080/medecube/rest/initial/pusher/auth',

auth: {

headers: {

'X-CSRF-Token': "12345"

}

}

});

var channel = pusher.subscribe('private-channel');

channel.bind('pusher:subscription_succeeded', function() {

var triggered = channel.trigger('client-myEvent', { "message": "i am pusher client2" });

});

</script>

</head>

Event Trigger by server:

Pusher : Event recd : {"event":"client-myEvent","data":{"message":"i am pusher client2"},"channel":"private-channel"}

Then both client get same response. I want only one user subscribe only 1 channel.

网友答案:

It is possible to get subscription count for private channels, you don't have to use a presence channel. However, to get subscription count for a private channel you have to explicitly enable that feature via your Pusher dashboard as it's not enabled by default:

https://pusher.com/docs/rest_api#method-get-channel

网友答案:

You need to use presence channels to get information about the members in a channel. In your case you could get the count of subscribers with var count = presenceChannel.members.count;.

分享给朋友:
您可能感兴趣的文章:
随机阅读: