Skip to content

Commit

Permalink
Merge pull request #332 from pusher/SubscriptionCountEvent
Browse files Browse the repository at this point in the history
Add Subscription count event
  • Loading branch information
singhashmeet authored Jul 15, 2022
2 parents fc4dd05 + 1db3852 commit 5c484c4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# pusher-websocket-java changelog

### Version 2.4.0 - 15th July 2022
* Add support for Subscription count events

### Version 2.3.0 - 4th July 2022
* Added support for user sign in and server to user messages
* Fixed issue with calling disconnect while the client is attempting reconnection
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The pusher-java-client is available in Maven Central.
<dependency>
<groupId>com.pusher</groupId>
<artifactId>pusher-java-client</artifactId>
<version>2.3.0-beta</version>
<version>2.4.0</version>
</dependency>
</dependencies>
```
Expand All @@ -83,7 +83,7 @@ The pusher-java-client is available in Maven Central.

```groovy
dependencies {
compile 'com.pusher:pusher-java-client:2.3.0-beta'
compile 'com.pusher:pusher-java-client:2.4.0'
}
```

Expand Down Expand Up @@ -256,6 +256,19 @@ Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() {

// Other ChannelEventListener methods
});
```
If you wish to be informed for subscription count events, use the `bind` function to listen to event type `pusher:subscription_count`:

```java
Channel channel = pusher.subscribe("my-channel");
channel.bind("pusher:subscription_count", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
System.out.println("Received event with data: " + event.toString());
System.out.println("Subscription Count is: " + channel.getCount());
}
});

```

### Private channels
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def getProperty = { property ->
}

group = "com.pusher"
version = "2.3.0-beta"
version = "2.4.0"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/pusher/client/channel/impl/BaseChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

Expand All @@ -11,19 +10,23 @@
import com.google.gson.GsonBuilder;
import com.pusher.client.channel.*;
import com.pusher.client.channel.impl.message.SubscribeMessage;
import com.pusher.client.channel.impl.message.SubscriptionCountData;
import com.pusher.client.channel.impl.message.UnsubscribeMessage;
import com.pusher.client.util.Factory;

public abstract class BaseChannel implements InternalChannel {
protected final Gson GSON;
private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:";
protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded";
protected static final String SUBSCRIPTION_COUNT_EVENT = "pusher_internal:subscription_count";
protected static final String PUBLIC_SUBSCRIPTION_COUNT_EVENT = "pusher:subscription_count";
private Set<SubscriptionEventListener> globalListeners = new HashSet<SubscriptionEventListener>();
private final Map<String, Set<SubscriptionEventListener>> eventNameToListenerMap = new HashMap<String, Set<SubscriptionEventListener>>();
protected volatile ChannelState state = ChannelState.INITIAL;
private ChannelEventListener eventListener;
private final Factory factory;
private final Object lock = new Object();
private Integer subscriptionCount;

public BaseChannel(final Factory factory) {
GsonBuilder gsonBuilder = new GsonBuilder();
Expand All @@ -37,6 +40,11 @@ public BaseChannel(final Factory factory) {
@Override
abstract public String getName();

@Override
public Integer getCount() {
return subscriptionCount;
}

@Override
public void bind(final String eventName, final SubscriptionEventListener listener) {
validateArguments(eventName, listener);
Expand Down Expand Up @@ -112,6 +120,8 @@ public PusherEvent prepareEvent(String event, String message) {
public void onMessage(String event, String message) {
if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) {
updateState(ChannelState.SUBSCRIBED);
}else if (event.equals(SUBSCRIPTION_COUNT_EVENT)) {
handleSubscriptionCountEvent(message);
} else {
final Set<SubscriptionEventListener> listeners = getInterestedListeners(event);
if (listeners != null) {
Expand Down Expand Up @@ -184,6 +194,12 @@ private void validateArguments(final String eventName, final SubscriptionEventLi
}
}

private void handleSubscriptionCountEvent(final String message) {
final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class);
subscriptionCount = subscriptionCountMessage.getCount();
onMessage(PUBLIC_SUBSCRIPTION_COUNT_EVENT, message);
}

protected Set<SubscriptionEventListener> getInterestedListeners(String event) {
synchronized (lock) {
Set<SubscriptionEventListener> listeners = new HashSet<SubscriptionEventListener>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface InternalChannel extends Channel, Comparable<InternalChannel> {

String toUnsubscribeMessage();

Integer getCount();

PusherEvent prepareEvent(String event, String message);

void onMessage(String event, String message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pusher.client.channel.impl.message;

import com.google.gson.annotations.SerializedName;

public class SubscriptionCountData {
@SerializedName("subscription_count")
public Integer count;

public int getCount(){
return count;
}
}

0 comments on commit 5c484c4

Please sign in to comment.