com.streamhub.client
Class StreamHubGWTAdapter

java.lang.Object
  extended by com.streamhub.client.StreamHubGWTAdapter

public class StreamHubGWTAdapter
extends Object

The main class for connecting to a StreamHub Push Server and subscribing to topics.

Example of the adapter in use:

StreamHubGWTAdapter hub = new StreamHubGWTAdapter();
hub.connect("http://push.example.com/");
hub.subscribe("GOOG", updateListener);
hub.unsubscribe("GOOG");
hub.disconnect(); 

The source code for the adapter and examples is available at: GWT Comet StreamHub on Google Code.


Constructor Summary
StreamHubGWTAdapter()
          Creates a new un-connected StreamHub GWT adapter
 
Method Summary
 void connect(FailoverParameters failoverParameters)
          Connects to the server with advanced failover configuration.
 void connect(String url)
          Connects to the server
 void disconnect()
          Disconnects from the StreamHub server.
static void onUpdate(String topic, com.google.gwt.core.client.JavaScriptObject update)
          This method is called internally everytime an update is received to a subscribed topic.
 void publish(String topic, com.google.gwt.json.client.JSONObject jsonObject)
          Publishes data on a particular topic.
 void subscribe(String[] topics, StreamHubGWTUpdateListener listener)
          Subscribes to an array of topics in a single request to the StreamHub server.
 void subscribe(String topic, StreamHubGWTUpdateListener listener)
          Subscribes to a particular topic.
 void unsubscribe(String topic)
          Unsubscribes from a particular topic
 void unsubscribe(String[] topics)
          Unsubscribes from an array of topics
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StreamHubGWTAdapter

public StreamHubGWTAdapter()
Creates a new un-connected StreamHub GWT adapter

Method Detail

onUpdate

public static void onUpdate(String topic,
                            com.google.gwt.core.client.JavaScriptObject update)
This method is called internally everytime an update is received to a subscribed topic. It notifies any StreamHubGWTUpdateListener that has been added via a call to subscribe(String, StreamHubGWTUpdateListener). It is useful for testing without connecting to a real server.

Parameters:
topic - the topic on which the update was received
update - the fields received in the update

connect

public void connect(String url)
Connects to the server

Parameters:
url - the URL of the server to connect to e.g. http://localhost:7979/. The trailing slash is important.

connect

public void connect(FailoverParameters failoverParameters)
Connects to the server with advanced failover configuration.

Parameters:
failoverParameters - the failover configuration parameters
See Also:
FailoverParameters

subscribe

public void subscribe(String topic,
                      StreamHubGWTUpdateListener listener)
Subscribes to a particular topic. The StreamHubGWTUpdateListener will be notified of any updates on this topic.

Parameters:
topic - the topic to subscribe to
listener - the listener to receive updates on this topic
See Also:
subscribe(String[], StreamHubGWTUpdateListener), StreamHubGWTUpdateListener

subscribe

public void subscribe(String[] topics,
                      StreamHubGWTUpdateListener listener)
Subscribes to an array of topics in a single request to the StreamHub server. This method should be preferred over subscribe(String, StreamHubGWTUpdateListener) where multiple topics are used as it reduces the number of HTTP requests involved. The StreamHubGWTUpdateListener will be notified of updates on any of the topics.

Parameters:
topics - the topics to subscribe to
listener - the listener to receive updates on ay of the topics
See Also:
subscribe(String, StreamHubGWTUpdateListener), StreamHubGWTUpdateListener

unsubscribe

public void unsubscribe(String topic)
Unsubscribes from a particular topic

Parameters:
topic - the topic to unsubscribe from

unsubscribe

public void unsubscribe(String[] topics)
Unsubscribes from an array of topics

Parameters:
topics - the topics to unsubscribe from

publish

public void publish(String topic,
                    com.google.gwt.json.client.JSONObject jsonObject)
Publishes data on a particular topic. The data contained in jsonObject will be sent back to the server. The server may choose to publish this data to all subscribed clients or instead return a custom response to this client.

Example of publishing data:

// Connect
StreamHubGWTAdapter hub = new StreamHubGWTAdapter();
hub.connect("http://push.example.com/");

// Create some data to publish
JSONObject payload = new JSONObject();
payload.put("Name", new JSONString("GWT-Fan"));
payload.put("Message", new JSONString("GWT is cool!"));

// Publish the data
hub.publish("ChatRoom", payload);

Parameters:
topic - the topic to publish the data on
jsonObject - the data to publish

disconnect

public void disconnect()
Disconnects from the StreamHub server. Note: this does not delete the subscriptions, use unsubscribe to remove the subscriptions.



Copyright © 2009 StreamHub. Tutorials, news and guides can be found on the StreamHub Comet Blog.