Pub/Sub Locations

Creating Users

Once our SDK is initialized, we need to create or get a user to start the tracking and use other methods. Each user created will have a unique Roam identifier which will be used later to login and access developer APIs. You can call it Roam userId.
Adding metadata to users is optional and you can pass null while creating users to ignore that field.
Kotlin
Java
// create user with meta-data and description
// Declare meta-data as json
// Description as string
val metadata = JSONObject()
metadata.put("key", "value")
Roam.createUser("SET-USER-DESCRIPTION-HERE", metadata, 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 the error code & message below
// roamError.getCode()
// roamError.getMessage()
}
})
// update user with meta-data and description
// Declare meta-data as dictionary
// Description as string
JSONObject metadata = new JSONObject();
metadata.put("key", "value");
Roam.createUser("YOUR-USER-DESCRIPTION-GOES-HERE", metadata, 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 "user description" option 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 and meta-data later using the code below.
Kotlin
Java
// update user with meta-data and description
// Declare meta-data as dictionary
// Description as string
val metadata = JSONObject()
metadata.put("key", "value")
Roam.setDescription("SET-USER-DESCRIPTION-HERE", metadata)
// update user with meta-data and description
// Declare meta-data as dictionary
// Description as string
JSONObject metadata = new JSONObject();
metadata.put("key", "value");
Roam.setDescription("SET-USER-DESCRIPTION-HERE", metadata);

Get User

If you already have a Roam userID that you would like to reuse instead of creating a new user, use the below to get the user session.
Kotlin
Java
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()
}
})
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 have to be changed to startTracking

Start Tracking

Kotlin
Java
Roam.startTracking(TrackingMode)
Roam.startTracking(TrackingMode);
Specify the tracking mode 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 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
// active tracking
Roam.startTracking(RoamTrackingMode.ACTIVE)
// balanced tracking
Roam.startTracking(RoamTrackingMode.BALANCED)
// passive tracking
Roam.startTracking(RoamTrackingMode.PASSIVE)
// active tracking
Roam.startTracking(RoamTrackingMode.ACTIVE);
// balanced tracking
Roam.startTracking(RoamTrackingMode.BALANCED);
// passive tracking
Roam.startTracking(RoamTrackingMode.PASSIVE);

Custom Tracking Modes

Our 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
// 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);
// 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
// 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)
// 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
Roam.stopTracking()
Roam.stopTracking();

Publish Locations

Publishing will help publish location data that will be stored in our database and sent to Roam servers for further processing.
Location data will be published and sent to the Roam servers for further processing. The data will be saved on our database servers.
Kotlin
Java
val locationData = RoamPublish.Builder().build()
Roam.publishAndSave(locationData)
RoamPublish locationData = new RoamPublish.Builder().build();
Roam.publishAndSave(locationData);
You can now send custom meta-data json values along with location data in publishAndSavemethod.
Kotlin
Java
val data = JSONObject()
data.put("Key", "Value")
val roamPublish = RoamPublish.Builder()
.metadata(data)
.build()
Roam.publishAndSave(roamPublish)
JSONObject data = new JSONObject();
data.put("Key", "Value");
RoamPublish roamPublish = new RoamPublish.Builder()
.metadata(data)
.build();
Roam.publishAndSave(roamPublish);
To stop publishing locations,
Kotlin
Java
Roam.stopPublishing()
Roam.stopPublishing();

Subscribe to 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
Roam.subscribe(TYPE, "ROAM-USER-ID");
Roam.subscribe(TYPE, "USER-ID");

UnSubscribe

Kotlin
Java
Roam.unSubscribe(TYPE, "ROAM-USER-ID")
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.
You can pass an empty user_id which is an optional field from v0.0.14 to unsubscribe from all the users.

Location Listener

Before adding listeners 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 be set to false and needs to be set to true in order to stream the location and event updates to the same device or other devices.
Kotlin
Java
Roam.toggleListener(locations, events, object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when toggle listener success
// access location listener status with roamUser.getLocationListenerStatus()
// access event listener status with roamUser.getEventListenerStatus()
}
​
override fun onFailure(roamError: RoamError) {
// do something when toggle listener failure
}
})
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
Roam.getListenerStatus(object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when get listener status success
// get location listener status with roamUser.getLocationListenerStatus
// get event listener status with roamUser.getEventListenerStatus
}
override fun onFailure(roamError: RoamError) {
// do something when get listener status failure
}
})
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
Roam.toggleEvents(geofence, trip, location, movingGeofence, object : RoamCallback {
override fun onSuccess(roamUser: RoamUser) {
// do something when toggle events success
// access location event status with roamUser.getLocationEvents()
// access geofence event status with roamUser.getGeofenceEvents()
// access trip events status with roamUser.getTripsEvents()
// get moving geofence event status with roamUser.getMovingGeofenceEvents()
}
​
override fun onFailure(error: roamError) {
// do something when toggle events failure
}
​
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
public class LocationReceiver : RoamReceiver() {
override fun onLocationUpdated(context: Context, roamLocation: RoamLocation) {
// receive own location updates here
// do something with location data using location
// receive own location updates here
// do something with location data using location
// roamLocation.getUserId();
// roamLocation.getActivity();
// roamLocation.getRecordedAt();
// roamLocation.getTimezoneOffset();
// roamLocation.getMetadata();
// roamLocation.getBatteryStatus();
// roamLocation.getNetworkStatus();
// roamLocation.getLocation().getLatitude();
// roamLocation.getLocation().getLongitude();
// roamLocation.getLocation().getBearing();
// roamLocation.getLocation().getAltitude();
// roamLocation.getLocation().getAccuracy();
// roamLocation.getLocation().getSpeed();
// roamLocation.getLocation().getProvider();
// roamLocation.getLocation().getTime();
// roamLocation.getLocation().getVerticalAccuracyMeters();
}
​
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:List<TripStatusListener>) {
// receive real time trip status here
// do something with trip status data
}
​
override fun onError(context: Context, roamError: RoamError) {
//access error data here
}
}
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,List<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
}
}
Export as PDF
Copy link
On this page
Creating Users
Get User
Location Tracking
Start Tracking
Tracking Modes
Custom Tracking Modes
Stop Tracking
Publish Locations
Subscribe to Messages
Subscribe
UnSubscribe
Location Listener