public class Pusher extends 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(String apiKey)
Creates a new instance of Pusher.
|
Pusher(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(String channelName) |
Connection |
getConnection()
Gets the underlying
Connection object that is being used by this
instance of Pusher. |
PresenceChannel |
getPresenceChannel(String channelName) |
PrivateChannel |
getPrivateChannel(String channelName) |
Channel |
subscribe(String channelName)
Subscribes to a public
Channel . |
Channel |
subscribe(String channelName,
ChannelEventListener listener,
String... eventNames)
Binds a
ChannelEventListener to the specified events and then
subscribes to a public Channel . |
PresenceChannel |
subscribePresence(String channelName)
Subscribes to a
PresenceChannel which
requires authentication. |
PresenceChannel |
subscribePresence(String channelName,
PresenceChannelEventListener listener,
String... eventNames)
Subscribes to a
PresenceChannel which
requires authentication. |
PrivateChannel |
subscribePrivate(String channelName)
Subscribes to a
PrivateChannel which
requires authentication. |
PrivateChannel |
subscribePrivate(String channelName,
PrivateChannelEventListener listener,
String... eventNames)
Subscribes to a
PrivateChannel which
requires authentication. |
void |
unsubscribe(String channelName)
Unsubscribes from a channel using via the name of the channel.
|
public Pusher(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(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()
ConnectionEventListener
s 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 ConnectionState
s to bind your
ConnectionEventListener
to before connecting to
Pusher. If you do not specify any ConnectionState
s
then your ConnectionEventListener
will be bound to all
connection events. This is equivalent to calling
connect(ConnectionEventListener, ConnectionState...)
with ConnectionState.ALL
.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(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(String channelName, ChannelEventListener listener, 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.IllegalArgumentException
- If any of the following are true:
subscribePrivate(String, PrivateChannelEventListener, String...)
instead of this method.ChannelEventListener
is null.public PrivateChannel subscribePrivate(String channelName)
PrivateChannel
which
requires authentication.channelName
- The name of the channel to subscribe to.PrivateChannel
representing the subscription.IllegalStateException
- if a Authorizer
has not been set
for the Pusher
instance via
Pusher(String, PusherOptions)
.public PrivateChannel subscribePrivate(String channelName, PrivateChannelEventListener listener, 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.IllegalStateException
- if a Authorizer
has not been set for the Pusher
instance via Pusher(String, PusherOptions)
.public PresenceChannel subscribePresence(String channelName)
PresenceChannel
which
requires authentication.channelName
- The name of the channel to subscribe to.PresenceChannel
representing the subscription.IllegalStateException
- if a Authorizer
has not been set
for the Pusher
instance via
Pusher(String, PusherOptions)
.public PresenceChannel subscribePresence(String channelName, PresenceChannelEventListener listener, 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.IllegalStateException
- if a Authorizer
has not been set for the Pusher
instance via Pusher(String, PusherOptions)
.public void unsubscribe(String channelName)
channelName
- the name of the channel to be unsubscribed from.public Channel getChannel(String channelName)
channelName
- The name of the public channel to be retrievedIllegalArgumentException
- if you try to retrieve a private or presence channel.public PrivateChannel getPrivateChannel(String channelName)
channelName
- The name of the private channel to be retrievedIllegalArgumentException
- if you try to retrieve a public or presence channel.public PresenceChannel getPresenceChannel(String channelName)
channelName
- The name of the presence channel to be retrievedIllegalArgumentException
- if you try to retrieve a public or private channel.Copyright © 2016 Pusher. All rights reserved.