Pub/Sub Locations

Creating Users

Once the SDK is initialized, we need to create or get a user to start the tracking and use other methods. Every user created will have a unique Roam identifier which will be used later to login and access developer APIs. We can call it as Roam userId.

Kotlin
Java
Kotlin
Roam.createUser("SET-USER-DESCRIPTION-HERE", object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
override fun onFailure(roamError: RoamError) {
// Access error code & message below
// roamError.getCode()
// roamError.getMessage()
}
})
Java
Roam.createUser("YOUR-USER-DESCRIPTION-GOES-HERE", new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
@Override
public void onFailure(RoamError roamError) {
// Access error code & message below
// roamError.getCode();
// roamError.getMessage();
}
});

The option user description can be used to update your user information such as name, address or add an existing user ID. Make sure the information is encrypted if you are planning to save personal user information like email or phone number.

You can always set or update user descriptions later using the below code.

Kotlin
Java
Kotlin
Roam.setDescription("SET-USER-DESCRIPTION-HERE")
Java
Roam.setDescription("SET-USER-DESCRIPTION-HERE");

Get User

If you already have a Roam userID which you would like to reuse instead of creating a new user, use the below to get user session.

Kotlin
Java
Kotlin
Roam.getUser("ROAM-USER-ID", object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
override fun onFailure(roamError: RoamError) {
// Access error code & message below
// roamError.getCode()
// roamError.getMessage()
}
})
Java
Roam.getUser("YOUR-ROAM-USER-ID", new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
@Override
public void onFailure(RoamError roamError) {
// Access error code & message below
// roamError.getCode();
// roamError.getMessage();
}
});

Location Tracking

To publish and subscribe to location data, startSelfTracking methods has to be changed to startTracking

Start Tracking

Kotlin
Java
Kotlin
Roam.startTracking(TrackingMode)
Java
Roam.startTracking(TrackingMode);

Use the tracking modes while you use the startTracking method Roam.startTracking

Tracking Modes

Roam has three default tracking modes along with a custom version. They differ based on the frequency of location updates and battery consumption. The higher the frequency, the higher is the battery consumption. You must use foreground service for continuous tracking.

Mode

Battery usage

Updates every

Optimised for/advised for

Active

6% - 12%

25 ~ 250 meters

Ride Hailing / Sharing

Balanced

3% - 6%

50 ~ 500 meters

On Demand Services

Passive

0% - 1%

100 ~ 1000 meters

Social Apps

Kotlin
Java
Kotlin
// active tracking
Roam.startTracking(RoamTrackingMode.ACTIVE)
// balanced tracking
Roam.startTracking(RoamTrackingMode.BALANCED)
// passive tracking
Roam.startTracking(RoamTrackingMode.PASSIVE)
Java
// active tracking
Roam.startTracking(RoamTrackingMode.ACTIVE);
// balanced tracking
Roam.startTracking(RoamTrackingMode.BALANCED);
// passive tracking
Roam.startTracking(RoamTrackingMode.PASSIVE);

Custom Tracking Modes

The SDK also provides a custom tracking mode that allows you to customize and build your own tracking modes.

Type

Unit

Unit Range

Distance Interval

Meters

1m ~ 2500m

Time Interval

Seconds

10s ~ 10800s

Distance between location updates example code:

Kotlin
Java
Kotlin
// Define a custom tracking method with desired distance interval, stop duration and accuracy
val trackingMode = RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>, <STOP-INTERVAL-IN-SECONDS>)
.setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH)
.build()
// Start the tracking with the above created custom tracking method
Roam.startTracking(trackingMode);
Java
// Define a custom tracking method with desired distance interval, stop duration and accuracy
RoamTrackingMode trackingMode = new RoamTrackingMode.Builder(<DISTANCE-FILTER-IN-METERS>, <STOP-INTERVAL-IN-SECONDS>)
.setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH)
.build();
// Start the tracking with the above created custom tracking method
Roam.startTracking(trackingMode);

Time between location updates example code:

Kotlin
Java
Kotlin
// Define a custom tracking method with desired time interval and accuracy
val trackingMode = RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>)
.setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH)
.build()
// Start the tracking with the above created custom tracking method
Roam.startTracking(trackingMode)
Java
// Define a custom tracking method with desired time interval and accuracy
RoamTrackingMode trackingMode = new RoamTrackingMode.Builder(<TIME-INTERVAL-IN-SECONDS>)
.setDesiredAccuracy(RoamTrackingMode.DesiredAccuracy.HIGH)
.build();
// Start the tracking with the above created custom tracking method
Roam.startTracking(trackingMode);

You may see a delay if the user's device is in low power mode or has connectivity issues.

Stop Tracking

To stop the tracking use the below method.

Kotlin
Java
Kotlin
Roam.stopTracking()
Java
Roam.stopTracking();

Publish Locations

It will publish location data and these data will be sent to Roam servers for further processing and data will be saved in our database servers.

Kotlin
Java
Kotlin
val locationData = RoamPublish.Builder().build()
Roam.publishAndSave(locationData)
Java
RoamPublish locationData = new RoamPublish.Builder().build();
Roam.publishAndSave(locationData);

To stop publishing locations.

Kotlin
Java
Kotlin
Roam.stopPublishing()
Java
Roam.stopPublishing();

Subscribe Messages

Now that you have enabled the location listener, use the below method to subscribe to your own or other user's location updates and events.

Subscribe

Kotlin
Java
Kotlin
Roam.subscribe(TYPE, "ROAM-USER-ID");
Java
GeoSpark.subscribe(TYPE, "USER-ID");

UnSubscribe

Kotlin
Java
Kotlin
Roam.unSubscribe(TYPE, "ROAM-USER-ID")
Java
Roam.unSubscribe(TYPE, "ROAM-USER-ID");

Type

Description

Roam.Subscribe.EVENTS

Subscribe to your own events.

Roam.Subscribe.LOCATION

Subscribe to your own location (or) other user's location updates.

Roam.Subscribe.BOTH

Subscribe to your own events and location (or) other user's location updates.

Location Listener

Before adding listerners for locations and other data, we need to toggle the listener flags for the user in order to get the data.

To do that, you need to set the location and event listener to true using the below method. By default, the status will set to false and needs to be set to true in order to stream the location and events updates to the same device or other devices.

Kotlin
Java
Kotlin
Roam.toggleListener(locations, events, object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when toggle listener success
// access locations listener status with roamUser.getLocationListenerStatus()
// access events listener status with roamUser.getEventListenerStatus()
}
override fun onFailure(roamError: RoamError) {
// do something when toggle listener failure
}
})
Java
Roam.toggleListener(true, true, new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
@Override
public void onFailure(RoamError roamError) {
// Access error code & message below
// roamError.getCode();
// roamError.getMessage();
}
});

You can also get the current status of listeners with the below method.

Kotlin
Java
Kotlin
Roam.getListenerStatus(object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when get listener status success
// get location listener status with roamUser.getLocationListenerStatus
// get events listener status with roamUser.getEventListenerStatus
}
override fun onFailure(roamError: RoamError) {
// do something when get listener status failure
}
})
Java
Roam.getListenerStatus(new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
@Override
public void onFailure(RoamError roamError) {
// Access error code & message below
// roamError.getCode();
// roamError.getMessage();
}
});

To listen to events on the server-side, you should enable events for the user using the method below.

Kotlin
Java
Kotlin
Roam.toggleEvents(geofence, trip, location, movingGeofence, object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when toggle events success
// access location events status with roamUser.getLocationEvents()
// access geofence events status with roamUser.getGeofenceEvents()
// access trips events status with roamUser.getTripsEvents()
// get moving geofence event status with roamUser.getMovingGeofenceEvents()
}
override fun onFailure(error: roamError) {
// do something when toggle events failure
}
Java
Roam.toggleEvents(true, true, true, true, new RoamCallback() {
@Override
public void onSuccess(RoamUser roamUser) {
// Access Roam user data below
// roamUser.getUserId();
// roamUser.getDescription();
// roamUser.getEventListenerStatus();
// roamUser.getLocationListenerStatus();
// roamUser.getLocationEvents();
// roamUser.getGeofenceEvents();
// roamUser.getMovingGeofenceEvents();
// roamUser.getTripsEvents();
}
@Override
public void onFailure(RoamError roamError) {
// Access error code & message below
// roamError.getCode();
// roamError.getMessage();
}
});

Now, to listen to location updates and events, create a class that extends RoamReceiver. Then register the receiver by adding a receiver element to the application element in your manifest.

<application>
...
<receiver android:name=".LocationReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.roam.android.RECEIVED"/>
</intent-filter>
</receiver>
...
</application>

Then add the code to the receiver.

Kotlin
Java
Kotlin
public class LocationReceiver : RoamReceiver() {
override fun onLocationUpdated(context: Context, roamLocation: RoamLocation) {
// receive own location updates here
// do something with location data using location
}
override fun onLocationReceived(context: Context, roamLocationReceived: RoamLocationReceived) {
// receive other user's location updates here
// do something with location
}
override fun onEventReceived(context: Context, roamEvent: RoamEvent) {
//access event data here
}
override fun onReceiveTripStatus(context: Context, listener: TripStatusListener) {
// receive real time trip status here
// do something with trip status data
}
override fun onError(context: Context, roamError: RoamError) {
//access error data here
}
}
Java
public class LocationReceiver extends RoamReceiver {
@Override
public void onLocationUpdated(Context context, RoamLocation roamLocation) {
// receive own location updates here
// do something with location data using location
}
@Override
public void onLocationReceived(Context context, RoamLocationReceived roamLocationReceived) {
// receive other user's location updates here
// do something with location
}
@Override
public void onEventReceived(Context context, RoamEvent roamEvent) {
//access event data here
}
@Override
public void onReceiveTripStatus(Context context, TripStatusListener listener) {
// receive real time trip status here
// do something with trip status data
}
@Override
public void onError(Context context, RoamError roamError) {
//access error data here
}
}