Class Pusher
- java.lang.Object
-
- com.pusher.client.Pusher
-
- All Implemented Interfaces:
Client
public class Pusher extends java.lang.Object implements Client
This class is the main entry point for accessing Pusher.By creating a new
Pusherinstance and callingconnect()a connection to Pusher is established.Subscriptions for data are represented by
Channelobjects, or subclasses thereof. Subscriptions are created by callingsubscribe(String),subscribePrivate(String),subscribePresence(String)or one of the overloads.
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconnect()Connects to Pusher.voidconnect(ConnectionEventListener eventListener, ConnectionState... connectionStates)Binds aConnectionEventListenerto the specified events and then connects to Pusher.voiddisconnect()Disconnect from Pusher.ChannelgetChannel(java.lang.String channelName)ConnectiongetConnection()Gets the underlyingConnectionobject that is being used by this instance of Pusher.PresenceChannelgetPresenceChannel(java.lang.String channelName)PrivateChannelgetPrivateChannel(java.lang.String channelName)PrivateEncryptedChannelgetPrivateEncryptedChannel(java.lang.String channelName)voidsignin()Signs in on the Pusher connection as the current user.Channelsubscribe(java.lang.String channelName)Subscribes to a publicChannel.Channelsubscribe(java.lang.String channelName, ChannelEventListener listener, java.lang.String... eventNames)Binds aChannelEventListenerto the specified events and then subscribes to a publicChannel.PresenceChannelsubscribePresence(java.lang.String channelName)Subscribes to aPresenceChannelwhich requires authentication.PresenceChannelsubscribePresence(java.lang.String channelName, PresenceChannelEventListener listener, java.lang.String... eventNames)Subscribes to aPresenceChannelwhich requires authentication.PrivateChannelsubscribePrivate(java.lang.String channelName)Subscribes to aPrivateChannelwhich requires authentication.PrivateChannelsubscribePrivate(java.lang.String channelName, PrivateChannelEventListener listener, java.lang.String... eventNames)Subscribes to aPrivateChannelwhich requires authentication.PrivateEncryptedChannelsubscribePrivateEncrypted(java.lang.String channelName, PrivateEncryptedChannelEventListener listener, java.lang.String... eventNames)Subscribes to aPrivateEncryptedChannelwhich requires authentication.voidunsubscribe(java.lang.String channelName)Unsubscribes from a channel using via the name of the channel.Useruser()
-
-
-
Constructor Detail
-
Pusher
public Pusher(java.lang.String apiKey)
Creates a new instance of Pusher.Note that if you use this constructor you will not be able to subscribe to private or presence channels because no
ChannelAuthorizerhas been set. If you want to use private or presence channels:- Create an implementation of the
ChannelAuthorizerinterface, or use theHttpChannelAuthorizerprovided. - Create an instance of
PusherOptionsand set the authorizer on it by callingPusherOptions.setChannelAuthorizer(ChannelAuthorizer). - Use the
Pusher(String, PusherOptions)constructor to create an instance of Pusher.
The
PrivateChannelExampleAppandPresenceChannelExampleAppexample applications show how to do this.- Parameters:
apiKey- Your Pusher API key.
- Create an implementation of the
-
Pusher
public Pusher(java.lang.String apiKey, PusherOptions pusherOptions)
Creates a new instance of Pusher.- Parameters:
apiKey- Your Pusher API key.pusherOptions- Options for the Pusher client library to use.
-
-
Method Detail
-
getConnection
public Connection getConnection()
Gets the underlyingConnectionobject that is being used by this instance of Pusher.- Specified by:
getConnectionin interfaceClient- Returns:
- The
Connectionobject.
-
connect
public void connect()
Connects to Pusher. AnyConnectionEventListeners that have already been registered using theConnection.bind(ConnectionState, ConnectionEventListener)method will receive connection events.Calls are ignored (a connection is not attempted) if the
Connection.getState()is notConnectionState.DISCONNECTEDorConnectionState.DISCONNECTING.
-
connect
public void connect(ConnectionEventListener eventListener, ConnectionState... connectionStates)
Binds aConnectionEventListenerto the specified events and then connects to Pusher. This is equivalent to binding aConnectionEventListenerusing theConnection.bind(ConnectionState, ConnectionEventListener)method before connecting.Calls are ignored (a connection is not attempted) if the
Connection.getState()is notConnectionState.DISCONNECTED.- Specified by:
connectin interfaceClient- Parameters:
eventListener- AConnectionEventListenerthat will receive connection events. This can be null if you are not interested in receiving connection events, in which case you should callconnect()instead of this method.connectionStates- An optional list ofConnectionStates to bind yourConnectionEventListenerto before connecting to Pusher. If you do not specify anyConnectionStates then yourConnectionEventListenerwill be bound to all connection events. This is equivalent to callingconnect(ConnectionEventListener, ConnectionState...)withConnectionState.ALL.- Throws:
java.lang.IllegalArgumentException- If theConnectionEventListeneris null and at least one connection state has been specified.
-
disconnect
public void disconnect()
Disconnect from Pusher.Calls are ignored if the
Connection.getState(), retrieved fromgetConnection(), isConnectionState.DISCONNECTINGorConnectionState.DISCONNECTED.- Specified by:
disconnectin interfaceClient
-
signin
public void signin()
Signs in on the Pusher connection as the current user.Requires
PusherOptions.setUserAuthenticator(com.pusher.client.UserAuthenticator)to have been called.- Throws:
java.lang.IllegalStateException- if noUserAuthenticatorhas been set.
-
subscribe
public Channel subscribe(java.lang.String channelName)
Subscribes to a publicChannel.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.
-
subscribe
public Channel subscribe(java.lang.String channelName, ChannelEventListener listener, java.lang.String... eventNames)
Binds aChannelEventListenerto the specified events and then subscribes to a publicChannel.- Specified by:
subscribein interfaceClient- Parameters:
channelName- The name of theChannelto subscribe to.listener- AChannelEventListenerto receive events. This can be null if you don't want to bind a listener at subscription time, in which case you should callsubscribe(String)instead of this method.eventNames- An optional list of event names to bind yourChannelEventListenerto before subscribing.- Returns:
- The
Channelobject representing your subscription. - Throws:
java.lang.IllegalArgumentException- If any of the following are true:- The channel name is null.
- You are already subscribed to this channel.
- The channel name starts with "private-". If you want to
subscribe to a private channel, call
subscribePrivate(String, PrivateChannelEventListener, String...)instead of this method. - At least one of the specified event names is null.
- You have specified at least one event name and your
ChannelEventListeneris null.
-
subscribePrivate
public PrivateChannel subscribePrivate(java.lang.String channelName)
Subscribes to aPrivateChannelwhich requires authentication.- Specified by:
subscribePrivatein interfaceClient- Parameters:
channelName- The name of the channel to subscribe to.- Returns:
- A new
PrivateChannelrepresenting the subscription. - Throws:
java.lang.IllegalStateException- if aChannelAuthorizerhas not been set for thePusherinstance viaPusher(String, PusherOptions).
-
subscribePrivate
public PrivateChannel subscribePrivate(java.lang.String channelName, PrivateChannelEventListener listener, java.lang.String... eventNames)
Subscribes to aPrivateChannelwhich requires authentication.- Specified by:
subscribePrivatein interfaceClient- Parameters:
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 callingChannel.bind(String, SubscriptionEventListener)one or more times.- Returns:
- A new
PrivateChannelrepresenting the subscription. - Throws:
java.lang.IllegalStateException- if aChannelAuthorizerhas not been set for thePusherinstance viaPusher(String, PusherOptions).
-
subscribePrivateEncrypted
public PrivateEncryptedChannel subscribePrivateEncrypted(java.lang.String channelName, PrivateEncryptedChannelEventListener listener, java.lang.String... eventNames)
Subscribes to aPrivateEncryptedChannelwhich requires authentication.- Parameters:
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 callingChannel.bind(String, SubscriptionEventListener)one or more times.- Returns:
- A new
PrivateEncryptedChannelrepresenting the subscription. - Throws:
java.lang.IllegalStateException- if aChannelAuthorizerhas not been set for thePusherinstance viaPusher(String, PusherOptions).
-
subscribePresence
public PresenceChannel subscribePresence(java.lang.String channelName)
Subscribes to aPresenceChannelwhich requires authentication.- Specified by:
subscribePresencein interfaceClient- Parameters:
channelName- The name of the channel to subscribe to.- Returns:
- A new
PresenceChannelrepresenting the subscription. - Throws:
java.lang.IllegalStateException- if aChannelAuthorizerhas not been set for thePusherinstance viaPusher(String, PusherOptions).
-
subscribePresence
public PresenceChannel subscribePresence(java.lang.String channelName, PresenceChannelEventListener listener, java.lang.String... eventNames)
Subscribes to aPresenceChannelwhich requires authentication.- Specified by:
subscribePresencein interfaceClient- Parameters:
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 callingChannel.bind(String, SubscriptionEventListener)one or more times.- Returns:
- A new
PresenceChannelrepresenting the subscription. - Throws:
java.lang.IllegalStateException- if aChannelAuthorizerhas not been set for thePusherinstance viaPusher(String, PusherOptions).
-
unsubscribe
public void unsubscribe(java.lang.String channelName)
Unsubscribes from a channel using via the name of the channel.- Specified by:
unsubscribein interfaceClient- Parameters:
channelName- the name of the channel to be unsubscribed from.
-
getChannel
public Channel getChannel(java.lang.String channelName)
- Specified by:
getChannelin interfaceClient- Parameters:
channelName- The name of the public channel to be retrieved- Returns:
- A public channel, or null if it could not be found
- Throws:
java.lang.IllegalArgumentException- if you try to retrieve a private or presence channel.
-
getPrivateChannel
public PrivateChannel getPrivateChannel(java.lang.String channelName)
- Specified by:
getPrivateChannelin interfaceClient- Parameters:
channelName- The name of the private channel to be retrieved- Returns:
- A private channel, or null if it could not be found
- Throws:
java.lang.IllegalArgumentException- if you try to retrieve a public or presence channel.
-
getPrivateEncryptedChannel
public PrivateEncryptedChannel getPrivateEncryptedChannel(java.lang.String channelName)
- Parameters:
channelName- The name of the private encrypted channel to be retrieved- Returns:
- A private encrypted channel, or null if it could not be found
- Throws:
java.lang.IllegalArgumentException- if you try to retrieve a public or presence channel.
-
getPresenceChannel
public PresenceChannel getPresenceChannel(java.lang.String channelName)
- Specified by:
getPresenceChannelin interfaceClient- Parameters:
channelName- The name of the presence channel to be retrieved- Returns:
- A presence channel, or null if it could not be found
- Throws:
java.lang.IllegalArgumentException- if you try to retrieve a public or private channel.
-
-