public class Pusher extends java.lang.Object
By creating a new Pusher instance and calling connect() a connection to Pusher is established.
Subscriptions for data are represented by
Channel objects, or subclasses thereof.
Subscriptions are created by calling subscribe(String),
subscribePrivate(String),
subscribePresence(String) or one of the overloads.
| Constructor and Description |
|---|
Pusher(java.lang.String apiKey)
Creates a new instance of Pusher.
|
Pusher(java.lang.String apiKey,
PusherOptions pusherOptions)
Creates a new instance of Pusher.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect()
Connects to Pusher.
|
void |
connect(ConnectionEventListener eventListener,
ConnectionState... connectionStates)
Binds a
ConnectionEventListener to the specified events and then
connects to Pusher. |
void |
disconnect()
Disconnect from Pusher.
|
Channel |
getChannel(java.lang.String channelName) |
Connection |
getConnection()
Gets the underlying
Connection object that is being used by this
instance of Pusher. |
PresenceChannel |
getPresenceChannel(java.lang.String channelName) |
PrivateChannel |
getPrivateChannel(java.lang.String channelName) |
Channel |
subscribe(java.lang.String channelName)
Subscribes to a public
Channel. |
Channel |
subscribe(java.lang.String channelName,
ChannelEventListener listener,
java.lang.String... eventNames)
Binds a
ChannelEventListener to the specified events and then
subscribes to a public Channel. |
PresenceChannel |
subscribePresence(java.lang.String channelName)
Subscribes to a
PresenceChannel which
requires authentication. |
PresenceChannel |
subscribePresence(java.lang.String channelName,
PresenceChannelEventListener listener,
java.lang.String... eventNames)
Subscribes to a
PresenceChannel which
requires authentication. |
PrivateChannel |
subscribePrivate(java.lang.String channelName)
Subscribes to a
PrivateChannel which
requires authentication. |
PrivateChannel |
subscribePrivate(java.lang.String channelName,
PrivateChannelEventListener listener,
java.lang.String... eventNames)
Subscribes to a
PrivateChannel which
requires authentication. |
void |
unsubscribe(java.lang.String channelName)
Unsubscribes from a channel using via the name of the channel.
|
public Pusher(java.lang.String apiKey)
Note that if you use this constructor you will not be able to subscribe
to private or presence channels because no Authorizer has been
set. If you want to use private or presence channels:
Authorizer interface, or use
the HttpAuthorizer provided.PusherOptions and set the authorizer on
it by calling PusherOptions.setAuthorizer(Authorizer).Pusher(String, PusherOptions) constructor to create
an instance of Pusher.
The PrivateChannelExampleApp and
PresenceChannelExampleApp example
applications show how to do this.
apiKey - Your Pusher API key.public Pusher(java.lang.String apiKey, PusherOptions pusherOptions)
apiKey - Your Pusher API key.pusherOptions - Options for the Pusher client library to use.public Connection getConnection()
Connection object that is being used by this
instance of Pusher.Connection object.public void connect()
ConnectionEventListeners that have
already been registered using the
Connection.bind(ConnectionState, ConnectionEventListener) method
will receive connection events.
Calls are ignored (a connection is not attempted) if the Connection.getState() is not ConnectionState.DISCONNECTED.
public void connect(ConnectionEventListener eventListener, ConnectionState... connectionStates)
ConnectionEventListener to the specified events and then
connects to Pusher. This is equivalent to binding a
ConnectionEventListener using the
Connection.bind(ConnectionState, ConnectionEventListener) method
before connecting.
Calls are ignored (a connection is not attempted) if the Connection.getState() is not ConnectionState.DISCONNECTED.
eventListener - A ConnectionEventListener that will receive connection
events. This can be null if you are not interested in
receiving connection events, in which case you should call
connect() instead of this method.connectionStates - An optional list of ConnectionStates to bind your
ConnectionEventListener to before connecting to
Pusher. If you do not specify any ConnectionStates
then your ConnectionEventListener will be bound to all
connection events. This is equivalent to calling
connect(ConnectionEventListener, ConnectionState...)
with ConnectionState.ALL.java.lang.IllegalArgumentException - If the ConnectionEventListener is null and at least
one connection state has been specified.public void disconnect()
Calls are ignored if the Connection.getState(), retrieved from getConnection(), is not
ConnectionState.CONNECTED.
public Channel subscribe(java.lang.String channelName)
Channel.
Note that subscriptions should be registered only once with a Pusher
instance. Subscriptions are persisted over disconnection and
re-registered with the server automatically on reconnection. This means
that subscriptions may also be registered before connect() is called,
they will be initiated on connection.public Channel subscribe(java.lang.String channelName, ChannelEventListener listener, java.lang.String... eventNames)
ChannelEventListener to the specified events and then
subscribes to a public Channel.channelName - The name of the Channel to subscribe to.listener - A ChannelEventListener to receive events. This can be
null if you don't want to bind a listener at subscription
time, in which case you should call subscribe(String)
instead of this method.eventNames - An optional list of event names to bind your
ChannelEventListener to before subscribing.Channel object representing your subscription.java.lang.IllegalArgumentException - If any of the following are true:
subscribePrivate(String, PrivateChannelEventListener, String...)
instead of this method.ChannelEventListener is null.public PrivateChannel subscribePrivate(java.lang.String channelName)
PrivateChannel which
requires authentication.channelName - The name of the channel to subscribe to.PrivateChannel
representing the subscription.java.lang.IllegalStateException - if a Authorizer has not been set
for the Pusher instance via
Pusher(String, PusherOptions).public PrivateChannel subscribePrivate(java.lang.String channelName, PrivateChannelEventListener listener, java.lang.String... eventNames)
PrivateChannel which
requires authentication.channelName - The name of the channel to subscribe to.listener - A listener to be informed of both Pusher channel protocol events and subscription data events.eventNames - An optional list of names of events to be bound to on the channel. The equivalent of calling Channel.bind(String, SubscriptionEventListener) on or more times.PrivateChannel representing the subscription.java.lang.IllegalStateException - if a Authorizer has not been set for the Pusher instance via Pusher(String, PusherOptions).public PresenceChannel subscribePresence(java.lang.String channelName)
PresenceChannel which
requires authentication.channelName - The name of the channel to subscribe to.PresenceChannel
representing the subscription.java.lang.IllegalStateException - if a Authorizer has not been set
for the Pusher instance via
Pusher(String, PusherOptions).public PresenceChannel subscribePresence(java.lang.String channelName, PresenceChannelEventListener listener, java.lang.String... eventNames)
PresenceChannel which
requires authentication.channelName - The name of the channel to subscribe to.listener - A listener to be informed of Pusher channel protocol, including presence-specific events, and subscription data events.eventNames - An optional list of names of events to be bound to on the channel. The equivalent of calling Channel.bind(String, SubscriptionEventListener) on or more times.PresenceChannel representing the subscription.java.lang.IllegalStateException - if a Authorizer has not been set for the Pusher instance via Pusher(String, PusherOptions).public void unsubscribe(java.lang.String channelName)
channelName - the name of the channel to be unsubscribed from.public Channel getChannel(java.lang.String channelName)
channelName - The name of the public channel to be retrievedjava.lang.IllegalArgumentException - if you try to retrieve a private or presence channel.public PrivateChannel getPrivateChannel(java.lang.String channelName)
channelName - The name of the private channel to be retrievedjava.lang.IllegalArgumentException - if you try to retrieve a public or presence channel.public PresenceChannel getPresenceChannel(java.lang.String channelName)
channelName - The name of the presence channel to be retrievedjava.lang.IllegalArgumentException - if you try to retrieve a public or private channel.