Skip to content

Commit 77b97fc

Browse files
authored
Document Example Apps (#254)
* Document the Example App and make simpler to follow * fix typo in private encrypted channel example * Document the private channels example * Document presence channel example app * Refactor the layout for private encrypted example app * Update example app class documentation
1 parent ea54af0 commit 77b97fc

File tree

4 files changed

+309
-235
lines changed

4 files changed

+309
-235
lines changed

src/main/java/com/pusher/client/example/ExampleApp.java

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,89 @@
66

77
import com.pusher.client.Pusher;
88
import com.pusher.client.PusherOptions;
9+
import com.pusher.client.channel.Channel;
910
import com.pusher.client.channel.ChannelEventListener;
1011
import com.pusher.client.channel.PusherEvent;
1112
import com.pusher.client.connection.ConnectionEventListener;
1213
import com.pusher.client.connection.ConnectionStateChange;
1314

14-
public class ExampleApp implements ConnectionEventListener, ChannelEventListener {
15+
/*
16+
This app demonstrates how to use a standard Pusher channel.
1517
16-
private final Pusher pusher;
17-
private final String channelName;
18-
private final String eventName;
19-
private final long startTime = System.currentTimeMillis();
18+
Please ensure you update this relevant parts below with your Pusher credentials before running.
19+
Your Pusher credentials can be found at https://dashboard.pusher.com, selecting the channels project,
20+
and visiting the App Keys tab.
21+
22+
For more specific information on how to use channels check out
23+
https://pusher.com/docs/channels/using_channels/channels
24+
*/
25+
26+
public class ExampleApp {
27+
28+
// make sure the following variables are configured for your instance:
29+
private String channelsKey = "FILL_ME_IN";
30+
private String channelName = "my-channel";
31+
private String eventName = "my-event";
32+
private String cluster = "eu";
2033

2134
public static void main(final String[] args) {
2235
new ExampleApp(args);
2336
}
2437

2538
public ExampleApp(final String[] args) {
2639

27-
final String apiKey = args.length > 0 ? args[0] : "161717a55e65825bacf1";
28-
channelName = args.length > 1 ? args[1] : "my-channel";
29-
eventName = args.length > 2 ? args[2] : "my-event";
40+
// if using from the command line, these variables need to be passed
41+
switch (args.length) {
42+
case 4: cluster = args[3];
43+
case 3: eventName = args[2];
44+
case 2: channelName = args[1];
45+
case 1: channelsKey = args[0];
46+
}
47+
48+
// configure your Pusher connection with the options you want
49+
final PusherOptions options = new PusherOptions()
50+
.setEncrypted(true)
51+
.setCluster(cluster);
52+
Pusher pusher = new Pusher(channelsKey, options);
53+
54+
// set up a ConnectionEventListener to listen for connection changes to Pusher
55+
ConnectionEventListener connectionEventListener = new ConnectionEventListener() {
56+
@Override
57+
public void onConnectionStateChange(ConnectionStateChange change) {
58+
System.out.println(String.format("Connection state changed from [%s] to [%s]",
59+
change.getPreviousState(), change.getCurrentState()));
60+
}
3061

31-
final PusherOptions options = new PusherOptions().setEncrypted(true);
32-
pusher = new Pusher(apiKey, options);
33-
pusher.connect(this);
62+
@Override
63+
public void onError(String message, String code, Exception e) {
64+
System.out.println(String.format("An error was received with message [%s], code [%s], exception [%s]",
65+
message, code, e));
66+
}
67+
};
3468

35-
pusher.subscribe(channelName, this, eventName);
69+
// connect to Pusher
70+
pusher.connect(connectionEventListener);
3671

37-
// Keep main thread asleep while we watch for events or application will
38-
// terminate
72+
// set up a ChannelEventListener to listen for messages to the channel and event we are interested in
73+
ChannelEventListener channelEventListener = new ChannelEventListener() {
74+
@Override
75+
public void onSubscriptionSucceeded(String channelName) {
76+
System.out.println(String.format(
77+
"Subscription to channel [%s] succeeded", channelName));
78+
}
79+
80+
@Override
81+
public void onEvent(PusherEvent event) {
82+
System.out.println(String.format(
83+
"Received event [%s]", event.toString()));
84+
}
85+
};
86+
87+
// subscribe to the channel and with the event listener for the event name
88+
Channel channel = pusher.subscribe(channelName, channelEventListener, eventName);
89+
90+
91+
// Keep main thread asleep while we watch for events or application will terminate
3992
while (true) {
4093
try {
4194
Thread.sleep(1000);
@@ -45,42 +98,4 @@ public ExampleApp(final String[] args) {
4598
}
4699
}
47100
}
48-
49-
/* ConnectionEventListener implementation */
50-
51-
@Override
52-
public void onConnectionStateChange(final ConnectionStateChange change) {
53-
54-
System.out.println(String.format("[%d] Connection state changed from [%s] to [%s]", timestamp(),
55-
change.getPreviousState(), change.getCurrentState()));
56-
}
57-
58-
@Override
59-
public void onError(final String message, final String code, final Exception e) {
60-
61-
System.out.println(String.format("[%d] An error was received with message [%s], code [%s], exception [%s]",
62-
timestamp(), message, code, e));
63-
}
64-
65-
/* ChannelEventListener implementation */
66-
67-
@Override
68-
public void onEvent(final PusherEvent event) {
69-
System.out.println(String.format("[%d] Received event [%s]", timestamp(), event.toString()));
70-
71-
final Gson gson = new Gson();
72-
@SuppressWarnings("unchecked")
73-
final Map<String, String> jsonObject = gson.fromJson(event.getData(), Map.class);
74-
System.out.println(jsonObject);
75-
}
76-
77-
@Override
78-
public void onSubscriptionSucceeded(final String channelName) {
79-
80-
System.out.println(String.format("[%d] Subscription to channel [%s] succeeded", timestamp(), channelName));
81-
}
82-
83-
private long timestamp() {
84-
return System.currentTimeMillis() - startTime;
85-
}
86101
}

src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java

Lines changed: 100 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.pusher.client.Pusher;
66
import com.pusher.client.PusherOptions;
7+
import com.pusher.client.channel.PrivateChannelEventListener;
78
import com.pusher.client.channel.PusherEvent;
89
import com.pusher.client.channel.PresenceChannel;
910
import com.pusher.client.channel.PresenceChannelEventListener;
@@ -12,112 +13,133 @@
1213
import com.pusher.client.connection.ConnectionStateChange;
1314
import com.pusher.client.util.HttpAuthorizer;
1415

15-
public class PresenceChannelExampleApp implements ConnectionEventListener, PresenceChannelEventListener {
16+
/*
17+
This app demonstrates how to use Presence Channels.
1618
17-
private final Pusher pusher;
18-
private final String channelName;
19-
private final String eventName;
19+
Please ensure you update this relevant parts below with your Pusher credentials before running.
20+
Your Pusher credentials can be found at https://dashboard.pusher.com, selecting the channels project,
21+
and visiting the App Keys tab.
22+
23+
A demonstration authorization endpoint using nodejs can be found
24+
https://github.com/pusher/pusher-channels-auth-example
25+
26+
For more information on private encrypted channels please read
27+
https://pusher.com/docs/channels/using_channels/presence-channels
28+
*/
29+
public class PresenceChannelExampleApp {
30+
31+
// make sure the following variables are configured for your instance:
32+
private String channelsKey = "FILL_ME_IN";
33+
private String channelName = "my-channel";
34+
private String eventName = "my-event";
35+
private String cluster = "eu";
36+
private String authorizationEndpoint = "http://localhost:3030/pusher/auth";
2037

2138
private final PresenceChannel channel;
2239

2340
public static void main(final String[] args) {
2441
new PresenceChannelExampleApp(args);
2542
}
2643

27-
public PresenceChannelExampleApp(final String[] args) {
28-
29-
final String apiKey = args.length > 0 ? args[0] : "a87fe72c6f36272aa4b1";
30-
channelName = args.length > 1 ? args[1] : "presence-my-channel";
31-
eventName = args.length > 2 ? args[2] : "my-event";
32-
33-
final HttpAuthorizer authorizer = new HttpAuthorizer(
34-
"http://www.leggetter.co.uk/pusher/pusher-examples/php/authentication/src/presence_auth.php");
35-
final PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true);
36-
37-
pusher = new Pusher(apiKey, options);
38-
pusher.connect(this);
44+
private PresenceChannelExampleApp(final String[] args) {
3945

40-
channel = pusher.subscribePresence(channelName, this, eventName);
41-
42-
// Keep main thread asleep while we watch for events or application will
43-
// terminate
44-
while (true) {
45-
try {
46-
Thread.sleep(1000);
47-
}
48-
catch (final InterruptedException e) {
49-
e.printStackTrace();
50-
}
46+
// if using from the command line, these variables need to be passed
47+
switch (args.length) {
48+
case 4: cluster = args[3];
49+
case 3: eventName = args[2];
50+
case 2: channelName = args[1];
51+
case 1: channelsKey = args[0];
5152
}
52-
}
53-
54-
/* ConnectionEventListener implementation */
55-
56-
@Override
57-
public void onConnectionStateChange(final ConnectionStateChange change) {
58-
59-
System.out.println(String.format("Connection state changed from [%s] to [%s]", change.getPreviousState(),
60-
change.getCurrentState()));
61-
}
62-
63-
@Override
64-
public void onError(final String message, final String code, final Exception e) {
65-
66-
System.out.println(String.format("An error was received with message [%s], code [%s], exception [%s]", message,
67-
code, e));
68-
}
69-
70-
/* PresenceChannelEventListener implementation */
71-
72-
@Override
73-
public void onUsersInformationReceived(final String channelName, final Set<User> users) {
7453

75-
System.out.println("Received user information");
76-
77-
printCurrentlySubscribedUsers();
78-
}
79-
80-
@Override
81-
public void userSubscribed(final String channelName, final User user) {
54+
// create a HttpAuthorizer that points to your authorization server
55+
final HttpAuthorizer authorizer = new HttpAuthorizer(authorizationEndpoint);
56+
57+
// configure your Pusher connection with the options you want
58+
final PusherOptions options = new PusherOptions()
59+
.setEncrypted(true)
60+
.setCluster(cluster)
61+
.setAuthorizer(authorizer);
62+
Pusher pusher = new Pusher(channelsKey, options);
63+
64+
// set up a ConnectionEventListener to listen for connection changes to Pusher
65+
ConnectionEventListener connectionEventListener = new ConnectionEventListener() {
66+
@Override
67+
public void onConnectionStateChange(ConnectionStateChange change) {
68+
System.out.println(String.format("Connection state changed from [%s] to [%s]",
69+
change.getPreviousState(), change.getCurrentState()));
70+
}
8271

83-
System.out.println(String.format("A new user has joined channel [%s]: %s", channelName, user.toString()));
72+
@Override
73+
public void onError(String message, String code, Exception e) {
74+
System.out.println(String.format("An error was received with message [%s], code [%s], exception [%s]",
75+
message, code, e));
76+
}
77+
};
8478

85-
printCurrentlySubscribedUsers();
86-
}
79+
// connect to Pusher
80+
pusher.connect(connectionEventListener);
8781

88-
@Override
89-
public void userUnsubscribed(final String channelName, final User user) {
82+
// set up a PresenceChannelEventListener to listen for messages to the channel and event we are interested in
83+
PresenceChannelEventListener presenceChannelEventListener = new PresenceChannelEventListener() {
84+
@Override
85+
public void onSubscriptionSucceeded(String channelName) {
86+
System.out.println(String.format(
87+
"Subscription to channel [%s] succeeded", channelName));
88+
}
9089

91-
System.out.println(String.format("A user has left channel [%s]: %s", channelName, user));
90+
@Override
91+
public void onEvent(PusherEvent event) {
92+
System.out.println(String.format(
93+
"Received event [%s]", event.toString()));
94+
}
9295

93-
printCurrentlySubscribedUsers();
94-
}
96+
@Override
97+
public void onAuthenticationFailure(String message, Exception e) {
98+
System.out.println(String.format(
99+
"Authentication failure due to [%s], exception was [%s]", message, e));
100+
}
95101

96-
@Override
97-
public void onEvent(final PusherEvent event) {
102+
@Override
103+
public void onUsersInformationReceived(String channelName, Set<User> users) {
104+
System.out.println("Received user information");
105+
printCurrentlySubscribedUsers();
106+
}
98107

99-
System.out.println(String.format("Received event [%s]", event.toString()));
100-
}
108+
@Override
109+
public void userSubscribed(String channelName, User user) {
110+
System.out.println(String.format("A new user has joined channel [%s]: %s", channelName, user.toString()));
111+
printCurrentlySubscribedUsers();
112+
}
101113

102-
@Override
103-
public void onSubscriptionSucceeded(final String channelName) {
114+
@Override
115+
public void userUnsubscribed(String channelName, User user) {
116+
System.out.println(String.format("A user has left channel [%s]: %s", channelName, user));
117+
printCurrentlySubscribedUsers();
118+
}
119+
};
104120

105-
System.out.println(String.format("Subscription to channel [%s] succeeded", channel.getName()));
106-
}
121+
// subscribe to the channel and with the event listener for the event name
122+
channel = pusher.subscribePresence(channelName, presenceChannelEventListener, eventName);
107123

108-
@Override
109-
public void onAuthenticationFailure(final String message, final Exception e) {
110124

111-
System.out.println(String.format("Authentication failure due to [%s], exception was [%s]", message, e));
125+
// Keep main thread asleep while we watch for events or application will terminate
126+
while (true) {
127+
try {
128+
Thread.sleep(1000);
129+
}
130+
catch (final InterruptedException e) {
131+
e.printStackTrace();
132+
}
133+
}
112134
}
113135

114136
private void printCurrentlySubscribedUsers() {
115137
final StringBuilder sb = new StringBuilder("Users now subscribed to the channel:");
116-
for (final User remainingUser : channel.getUsers()) {
138+
for (final User user : channel.getUsers()) {
117139
sb.append("\n\t");
118-
sb.append(remainingUser.toString());
140+
sb.append(user.toString());
119141

120-
if (remainingUser.equals(channel.getMe())) {
142+
if (user.equals(channel.getMe())) {
121143
sb.append(" (me)");
122144
}
123145
}

0 commit comments

Comments
 (0)