user6328945
user6328945

Reputation:

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.

Upvotes: 1

Views: 2649

Answers (3)

AlexioVay
AlexioVay

Reputation: 4527

Check out my example of a private chat of 2 users where I validate if one user is currently typing or recording a voice message:

channel.pusher.channel('private-chat.{{ $thread_id }}')
    .bind("pusher:subscription_count", (data) => {
        let activeUsers = data.subscription_count;
        console.log(activeUsers);
        if (activeUsers === 1) {
            @this.recordingVoice = false;
            @this.typing = false;
        }
    });

I'm using Laravel Echo/Pusher with Livewire to solve this. I send the php variables with @this.recordingVoice = false to the Livewire component that stops displaying User X is currently recording a voice message... when the other user is leaving the chat room/dm inbox.

Upvotes: 0

Will Sewell
Will Sewell

Reputation: 2643

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;.

Upvotes: 1

DrewEaster
DrewEaster

Reputation: 3426

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

Upvotes: 2

Related Questions