### Run Pusher Java Example Application Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to execute the compiled example application JAR. By default, it connects to a sample application and subscribes to 'my-channel', listening for 'my-event'. ```Shell java -jar pusher-websocket-java-with-dependencies--jar-with-dependencies.jar ``` -------------------------------- ### Run Pusher Java Example Application with Custom Parameters Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to run the example application, allowing specification of `appKey`, `channelName`, and `eventName` as command-line arguments to override default connection settings. ```Shell java -jar pusher-websocket-java-with-dependencies--jar-with-dependencies.jar [appKey] [channelName] [eventName] ``` -------------------------------- ### Implementing Connection Event Listener in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Shows how to implement the `ConnectionEventListener` interface to receive notifications about changes in the Pusher connection state and errors. It includes an example of initializing Pusher with a custom listener. ```Java PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER); Pusher pusher = new Pusher(YOUR_APP_KEY, options); pusher.connect(new ConnectionEventListener() { @Override public void onConnectionStateChange(ConnectionStateChange change) { System.out.println("State changed to " + change.getCurrentState() + " from " + change.getPreviousState()); } @Override public void onError(String message, String code, Exception e) { System.out.println("There was a problem connecting!"); } }); ``` -------------------------------- ### Install Pusher Java Client with Gradle Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates how to include the Pusher Java client library as an implementation dependency in a Gradle project's build.gradle file. It uses the standard groupId:artifactId:version format. ```Groovy dependencies { implementation 'com.pusher:pusher-java-client:2.4.4' } ``` -------------------------------- ### Install Pusher Java Client with Maven Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet shows how to add the Pusher Java client library as a dependency in a Maven project's pom.xml file. It specifies the groupId, artifactId, and version for the library. ```XML com.pusher pusher-java-client ``` -------------------------------- ### Get a Presence Channel by Name (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Illustrates how to retrieve a `PresenceChannel` object using its name. The method requires the channel name to be prefixed with 'presence-' and will raise an exception otherwise. ```java PresenceChannel channel = pusher.getPresenceChannel("presence-channel"); ``` -------------------------------- ### Manage Multiple Event Listeners on a Channel (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Provides an example of a class that manages subscriptions and event bindings for a Pusher channel. It demonstrates how to bind and unbind different events (`my_event`, `my_other_event`) using the same `ChannelEventListener` instance. ```java public class Example implements ChannelEventListener { private final Pusher pusher; private final Channel channel; public Example() { pusher = new Pusher(YOUR_APP_KEY); channel = pusher.subscribe("my-channel", this, "my_event"); pusher.connect(); } public void listenToOtherEvent() { channel.bind("my_other_event", this); } public void stopListeningToOtherEvent() { channel.unbind("my_other_event", this); } } ``` -------------------------------- ### Handle Presence Channel Events with PresenceChannelEventListener in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This example illustrates how to implement the PresenceChannelEventListener interface to listen for user-related events on a presence channel. It includes overridden methods for onUsersInformationReceived, userSubscribed, and userUnsubscribed, providing callbacks for user presence changes. ```java PresenceChannel channel = pusher.subscribePresence("presence-channel", new PresenceChannelEventListener() { @Override public void onUsersInformationReceived(String channelName, Set users) { for (User user : users) { userSubscribed(channelName, user); } } @Override public void userSubscribed(String channelName, User user) { System.out.println( String.format("A new user joined channel [%s]: %s, %s", channelName, user.getId(), user.getInfo()) ); if (user.equals(channel.getMe())) { System.out.println("me"); } } @Override public void userUnsubscribed(String channelName, User user) { System.out.println( String.format("A user left channel [%s]: %s %s", channelName, user.getId(), user.getInfo()) ); } // Other ChannelEventListener methods }); ``` -------------------------------- ### Get a Basic Channel by Name (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Demonstrates how to retrieve a basic `Channel` object using its name. The method will raise an exception if the channel name is prefixed with 'private-' or 'presence-'. ```java Channel channel = pusher.getChannel("my-channel"); ``` -------------------------------- ### Get a Private Channel by Name (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Shows how to retrieve a `PrivateChannel` object using its name. The method requires the channel name to be prefixed with 'private-' and will raise an exception otherwise. ```java PrivateChannel channel = pusher.getPrivateChannel("private-channel"); ``` -------------------------------- ### Handle Public Channel Subscription Success in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md To be notified when a public channel subscription succeeds, implement the `ChannelEventListener` interface. This example shows how to override `onSubscriptionSucceeded` to log the successful subscription, ensuring the client is aware of the channel's readiness. ```Java Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { @Override public void onSubscriptionSucceeded(String channelName) { System.out.println("Subscribed to channel: " + channelName); } // Other ChannelEventListener methods }); ``` -------------------------------- ### Handle Private Channel Authentication Failure in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md When subscribing to private channels, it's crucial to handle potential authentication failures. This example demonstrates implementing `PrivateChannelEventListener` to catch `onAuthenticationFailure` events, providing details on why authorization failed and allowing for error recovery. ```Java PrivateChannel channel = pusher.subscribePrivate("private-channel", new PrivateChannelEventListener() { @Override public void onAuthenticationFailure(String message, Exception e) { System.out.println( String.format("Authentication failure due to [%s], exception was [%s]", message, e) ); } // Other ChannelEventListener methods }); ``` -------------------------------- ### Extract User ID from Client Events in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This example shows how to retrieve the user ID associated with a client-triggered event. By calling getUserId() on the PusherEvent object within a SubscriptionEventListener, applications can verify the authenticity of the event's sender. ```java channel.bind("client-my-event", new SubscriptionEventListener() { @Override public void onEvent(PusherEvent event) { System.out.println("Received event with userId: " + event.getUserId()); } }); ``` -------------------------------- ### Clone Pusher Java Library Project Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Instructions to clone the `pusher-websocket-java` repository from GitHub and navigate into its directory. ```Shell git clone https://github.com/pusher/pusher-websocket-java cd pusher-websocket-java ``` -------------------------------- ### Build All Artifacts and Run Tests for Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to perform a full build of the `pusher-websocket-java` library, including generating all JARs and executing all tests and verification steps. The compiled JARs will be found in the `build/libs` directory. ```Shell gradlew build ``` -------------------------------- ### Initialize and Connect Pusher Client in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates the full lifecycle of a Pusher client in Java, including initialization with options, connecting to the service, handling connection state changes and errors, subscribing to a channel, binding to specific events, and managing disconnection and reconnection. ```java // Create a new Pusher instance PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER); Pusher pusher = new Pusher(YOUR_APP_KEY, options); pusher.connect(new ConnectionEventListener() { @Override public void onConnectionStateChange(ConnectionStateChange change) { System.out.println("State changed to " + change.getCurrentState() + " from " + change.getPreviousState()); } @Override public void onError(String message, String code, Exception e) { System.out.println("There was a problem connecting!"); } }, ConnectionState.ALL); // Subscribe to a channel Channel channel = pusher.subscribe("my-channel"); // Bind to listen for events called "my-event" sent to "my-channel" channel.bind("my-event", new SubscriptionEventListener() { @Override public void onEvent(PusherEvent event) { System.out.println("Received event with data: " + event.toString()); } }); // Disconnect from the service pusher.disconnect(); // Reconnect, with all channel subscriptions and event bindings automatically recreated pusher.connect(); // The state change listener is notified when the connection has been re-established, // the subscription to "my-channel" and binding on "my-event" still exist. ``` -------------------------------- ### Assemble Artifacts for Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to assemble all build artifacts (e.g., JARs) for the `pusher-websocket-java` library without executing any tests. ```Shell gradlew assemble ``` -------------------------------- ### Basic Pusher Client Initialization in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet shows how to create a basic Pusher client instance in Java using an application key and cluster options. It's suitable for public channels where no special authorization is required. ```java PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER); Pusher pusher = new Pusher(YOUR_APP_KEY, options); ``` -------------------------------- ### Connect Pusher Client to Channels Service in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates the simplest way to establish a connection to the Pusher Channels service in Java. It initializes the Pusher client with basic options and then calls the connect() method. ```java PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER); Pusher pusher = new Pusher(YOUR_APP_KEY, options); pusher.connect(); ``` -------------------------------- ### Generate Eclipse Project Files for Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to generate `.classpath` and `.project` files required for importing the `pusher-websocket-java` project into Eclipse. ```Shell gradlew eclipse ``` -------------------------------- ### Generate Javadoc for Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to generate API documentation in Javadoc format for the `pusher-websocket-java` library. The output will be located in the `build/docs/javadoc/` directory. ```Shell gradlew javadoc ``` -------------------------------- ### Execute Tests for Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Command to run all unit and integration tests defined for the `pusher-websocket-java` library. ```Shell gradlew test ``` -------------------------------- ### Implement ChannelEventListener for Protocol and Application Events in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates how to subscribe to a channel and implement the ChannelEventListener to handle both subscription success events and specific application events. It shows how to bind to multiple event names ('foo', 'bar', 'baz') simultaneously. ```java Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { @Override public void onSubscriptionSucceeded(String channelName) { System.out.println("Subscribed!"); } @Override public void onEvent(PusherEvent event) { // Called for incoming events names "foo", "bar" or "baz" } }, "foo", "bar", "baz"); ``` -------------------------------- ### PusherOptions Object Configuration Methods Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Details the methods available on the `PusherOptions` object for configuring the Pusher client, including connection security, host, port, cluster, timeouts, and proxy settings. ```APIDOC PusherOptions: setEncrypted(Boolean): Sets whether the connection should be made with TLS or not. setChannelAuthorizer(ChannelAuthorizer): Sets the channel authorizer to be used when authorizing private and presence channels. setHost(String): The host to which connections will be made. setWsPort(int): The port to which unencrypted connections will be made. Automatically set correctly. setWssPort(int): The port to which encrypted connections will be made. Automatically set correctly. setCluster(String): Sets the cluster the client will connect to, thereby setting the Host and Port correctly. setActivityTimeout(long): The number of milliseconds of inactivity at which a "ping" will be triggered to check the connection. The default value is 120,000. setPongTimeout(long): The number of milliseconds the client waits to receive a "pong" response from the server before disconnecting. The default value is 30,000. setMaxReconnectionAttempts(int): Number of reconnection attempts that will be made when pusher.connect() is called, after which the client will give up. setMaxReconnectGapInSeconds(int): The delay in two reconnection extends exponentially (1, 2, 4, .. seconds) This property sets the maximum inbetween two reconnection attempts. setProxy(Proxy): Specify a proxy, e.g. options.setProxy( new Proxy( Proxy.Type.HTTP, new InetSocketAddress( "proxyaddress", 80 ) ) ) ``` -------------------------------- ### Configure Android Studio for Local Pusher Java Library Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Explains how to include the local `pusher-websocket-java` project in an Android app's `settings.gradle` file, allowing the app to use the local source code. ```Gradle include ':pusher-websocket-java' project(':pusher-websocket-java').projectDir = new File('/pusher-websocket-java') ``` -------------------------------- ### Subscribe to a Presence Channel in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates the basic method for subscribing to a presence channel using the Pusher Java client. It initializes a PresenceChannel object, enabling the application to receive presence-related events. ```java PresenceChannel presenceChannel = pusher.subscribePresence( "presence-channel" ); ``` -------------------------------- ### Trigger a Client Event on a Channel (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Demonstrates how to trigger a client event on a channel. Client events must have a `client-` prefix and are subject to specific rules like rate limits and requiring a successful subscription. ```java channel.trigger("client-myEvent", "{\"myName\":\"Bob\"}"); ``` -------------------------------- ### Subscribe to a Public Channel in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates the simplest way to subscribe to a public channel using the Pusher Java client. The `subscribe` method registers the channel with the Pusher instance, and subscriptions are automatically re-established on reconnect. ```Java Channel channel = pusher.subscribe("my-channel"); ``` -------------------------------- ### Deserialize User Information JSON with Gson in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet demonstrates how to deserialize the JSON string obtained from user.getInfo() into a Java object using the Gson library. It shows the steps to parse the arbitrary user information into a structured UserInfo class. ```java String jsonInfo = user.getInfo(); Gson gson = new Gson(); UserInfo info = gson.fromJson(jsonInfo, UserInfo.class); ``` -------------------------------- ### Add Local Pusher Java Library Dependency in Android Studio Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Shows how to add the local `pusher-websocket-java` project as an `implementation` dependency in an Android app's `build.gradle` file, replacing the standard SDK dependency. ```Gradle dependencies { implementation project(':pusher-websocket-java') } ``` -------------------------------- ### Pusher ChannelEventListener Interface API Reference Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This section describes the ChannelEventListener interface, which provides callbacks for both protocol-related events (like subscription success) and application data events. It extends SubscriptionEventListener. ```APIDOC ChannelEventListener Interface: Extends: SubscriptionEventListener Methods: onSubscriptionSucceeded(channelName: string): void Description: Called when the channel subscription successfully completes. onEvent(event: PusherEvent): void Description: Called for incoming application events on the channel, such as those named "foo", "bar", or "baz" if specified during subscription. ``` -------------------------------- ### Initialize Pusher Client with Channel Authorizer in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet illustrates how to initialize a Pusher client in Java with a custom ChannelAuthorizer for private or presence channels. It configures PusherOptions to include an HTTP authorizer endpoint for authentication. ```java HttpChannelAuthorizer channelAuthorizer = new HttpChannelAuthorizer("http://example.com/some_auth_endpoint"); PusherOptions options = new PusherOptions().setCluster(YOUR_APP_CLUSTER).setChannelAuthorizer(channelAuthorizer); Pusher pusher = new Pusher(YOUR_APP_KEY, options); ``` -------------------------------- ### Monitor Public Channel Subscription Count in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This code demonstrates how to bind to the `pusher:subscription_count` event on a public channel. By implementing a `SubscriptionEventListener`, you can receive updates on the number of active subscribers, allowing for real-time monitoring of channel engagement. ```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()); } }); ``` -------------------------------- ### Subscribe to a Private Channel in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Subscribing to private channels requires a `ChannelAuthorizer` configured with the `Pusher` instance to handle authorization. This snippet shows the basic `subscribePrivate` call, which initiates the authorization process before establishing the channel connection. ```Java PrivateChannel privateChannel = pusher.subscribePrivate( "private-channel" ); ``` -------------------------------- ### Pusher User Object API Reference Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This section describes the User object, which represents a subscriber on a presence channel. It details the primary methods available for retrieving user information, including a unique identifier and arbitrary additional data. ```APIDOC User Object: getId(): string Description: Fetches a unique identifier for the user on the presence channel. getInfo(): string Description: Fetches a string representing arbitrary additional information about the user in the form of a JSON hash, e.g., {"user_name":"Mr. User","user_score":1357}. ``` -------------------------------- ### Deserialize Pusher Event Data with Gson (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Illustrates how to implement `ChannelEventListener` to process incoming Pusher events. It shows how to use the Gson library to deserialize the event data into a custom `EventExample` class, assuming the data is in JSON format. ```java public class Example implements ChannelEventListener { public Example() { Pusher pusher = new Pusher(YOUR_APP_KEY); pusher.subscribe("my-channel", this); pusher.connect(); } @Override public void onEvent(PusherEvent event) { Gson gson = new Gson(); EventExample exampleEvent = gson.fromJson(event.getData(), EventExample.class); } } class EventExample { private int value1 = 1; private String value2 = "abc"; private transient int value3 = 3; EventExample() { } } ``` -------------------------------- ### Bind to a Specific Event on a Channel (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Demonstrates how to subscribe to a channel and bind a `ChannelEventListener` to a specific event named 'my-event'. The `onEvent` method will be called when the event is received. ```java Channel channel = pusher.subscribe("my-channel"); channel.bind("my-event", new ChannelEventListener() { @Override public void onEvent(PusherEvent event) { // Called for incoming events named "my-event" } }); ``` -------------------------------- ### Filtering Pusher Connection State Notifications in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Illustrates how to filter connection state change notifications by specifying `ConnectionState` members when registering the listener, ensuring only specific state transitions trigger the callback. ```Java // MyConnectionEventListener is notified only of transitions to the disconnected state pusher.connect(new MyConnectionEventListener(), ConnectionState.DISCONNECTED); ``` -------------------------------- ### Trigger Client Event on Private Channel After Subscription (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Shows how to trigger a client event on a private channel specifically after the subscription has successfully completed, using a `PrivateChannelEventListener`. ```java PrivateChannel channel = pusher.subscribePrivate("private-channel", new PrivateChannelEventListener() { @Override public void onSubscriptionSucceeded(String channelName) { channel.trigger("client-myEvent", "{\"myName\":\"Bob\"}"); } // Other PrivateChannelEventListener methods }); ``` -------------------------------- ### Subscribe to a Private Encrypted Channel in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md This snippet illustrates how to subscribe to a private encrypted channel, enabling end-to-end encryption for messages. It requires a `PrivateEncryptedChannelEventListener` and specifies the events of interest, ensuring secure communication where only authorized clients can decrypt messages. ```Java PrivateEncryptedChannel privateEncryptedChannel = pusher.subscribePrivateEncrypted("private-encrypted-channel", listener, "my-event"); ``` -------------------------------- ### Check if a Channel is Subscribed (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Demonstrates how to check the subscription status of a channel using the `isSubscribed()` method, which returns `true` or `false`. ```java Channel channel = pusher.getChannel("my-channel"); channel.isSubscribed(); // => `true`/`false` ``` -------------------------------- ### Disconnecting Pusher Client in Java Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Demonstrates how to explicitly disconnect the Pusher client, releasing all internal resources like threads and network connections. ```Java pusher.disconnect(); ``` -------------------------------- ### Access the Connection Socket ID (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Explains how to retrieve the unique `socket_id` for the current client's connection. This ID is available only after the connection has been established. ```java String socketId = pusher.getConnection().getSocketId(); ``` -------------------------------- ### Unbind an Event Listener from a Channel (Java) Source: https://github.com/pusher/pusher-websocket-java/blob/master/README.md Shows how to remove a previously bound event listener from a channel for a specific event. ```java channel.unbind("my_event", listener); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.