Reputation: 2099
We have this android app where we use Pusher to relay realtime updates from our servers to android clients.
The problem is if we add the following code snippet to all our activity pages, Pusher opens a new web socket for each page.
How can do one connection to pusher and use it on all our activities?
String apiKey = "abcde";
PusherOptions options = (new PusherOptions()).setEncrypted(false);
Pusher ph = new Pusher(apiKey, options);
ph.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);
if(pusher_channel != null){
Channel channel = ph.subscribe(pusher_channel);
// Bind to listen for events called "my-event" sent to "my-channel"
channel.bind("user_is_typing", new SubscriptionEventListener() {
@Override
public void onEvent(String channel, String event, String data) {
//System.out.println("Received event with data: " + data);
}
});
}
Upvotes: 1
Views: 297
Reputation: 2099
We resolved this issue by putting the connection scripts under:
public class MyApplication extends MultiDexApplication
Now our app opens only one websocket.
Upvotes: 1