Backbeam.io documentation

Realtime

If you want to use the realtime API first of all you nead to enable it just calling this method

Backbeam.enableRealTime();

This keeps a connection between the application and the server. At any moment you can also disable the real-time connection calling:

Backbeam.disableRealTime();

Subscribing to real time events

You can listen to events generated by other devices (or the server). In order to support subscribing only to certain events all real-time events have a name. Events have also a collection of key-value pairs with more information about that event. This is a sample code that listens to events named chat-room and prints the value of their text parameter.

RealTimeEventListener listener = new RealTimeEventListener() {
  @Override
  public void realTimeEventReceived(String name, Map<String, String> message) {
    System.out.println("event received: "+name+" "+message.get("text"));
  }
};
Backbeam.subscribeToRealTimeEvents("chat-room", listener);

If you are no longer interested in some kind of events you should unsubscribe from those events. It is a good practique to do so because this way the SDK stops having a reference to the listener. Otherwise you could start leaking memory. And it also helps to reduce the bandwidth usage because the device will no longer receive those type of events.

Backbeam.unsubscribeFromRealTimeEvents("chat-room", listener);

If the realtime connection is broken and it reconnects the SDK will re-subscribe automatically to the previously subscribed events so you don't have to re-subscribe programatically.

Sending real time events

Sending events is also very easy. Just indicate the event name and the data you want to send to the devices subscribed.

Map<String, String> message = new HashMap<String, String>();
message.put("text", "Hello world");
Backbeam.sendRealTimeEvent("chat-room", message);

Listening to real time connection changes

You may need to indicate the user whether the realtime connection is available or not. For example you could present the user an activity indicator while the connection is being established.

Backbeam.addRealTimeConnectionListener(new RealTimeConnectionListener() {

  @Override
  public void realTimeConnecting() {
    System.out.println("connecting...");
  }

  @Override
  public void realTimeConnected() {
    System.out.println("connected!");
  }

  @Override
  public void realTimeDisconnected() {
    System.out.println("disconnected!");
  }

  @Override
  public void realTimeConnectionFailed(Exception e) {
    System.out.println("connection failed");
    e.printStackTrace();
  }

});