Location Sharing

Location sharing allows users to send a static position or share their real-time location with other participants in a channel. Stream Chat supports both static and live location sharing.

There are two types of location sharing:

  • Static Location: A one-time location share that does not update over time.
  • Live Location: A real-time location sharing that updates over time.

The SDK handles location message creation and updates, but location tracking must be implemented by the application using device location services.

Enabling location sharing

The location sharing feature must be activated at the channel level before it can be used. You have two configuration options: activate it for a single channel using configuration overrides, or enable it globally for all channels of a particular type.

// Enabling it for a channel
await channel.updatePartial({
  set: {
    config_overrides: {
      shared_locations: true,
    },
  },
});

// Enabling it for a channel type
const update = await client.updateChannelType("messaging", {
  shared_locations: true,
});

// Enabling it for a newly created channel type
await serverClient.createChannelType({
  name: "shared_locations_channel_type",
  shared_locations: true,
});

Sending static location

Static location sharing allows you to send a message containing a static location.

final locationCoordinates = LocationCoordinates(
  latitude: 37.7749,
  longitude: -122.4194,
);

// Send a static location message.
final response = await channel.sendStaticLocation(
  location: locationCoordinates,
  createdByDeviceId: 'test_device_id', // Optional
);

Starting live location sharing

Live location sharing enables real-time location updates for a specified duration. The SDK manages the location message lifecycle, but your application is responsible for providing location updates.

final initialLocation = LocationCoordinates(
  latitude: 37.7749,
  longitude: -122.4194,
);

// Send a live location message with an end time.
final response = await channel.startLiveLocationSharing(
  location: initialLocation,
  endSharingAt: DateTime.timestamp().add(Duration(hours: 1)), // 1 hour later
  createdByDeviceId: 'test_device_id', // Optional
);

Stopping live location sharing

You can stop live location sharing for a specific message using the message controller:

// Stop live location sharing for a specific message.
final location = await client.stopLiveLocation(
  messageId: liveLocationMessageId,
  createdByDeviceId: 'test_device_id', // Optional
);

Updating live location

Your application must implement location tracking and provide updates to the SDK. The SDK handles updating all the current user’s active live location messages and provides a throttling mechanism to prevent excessive API calls.

// Update the user's live location (e.g., when device location changes)
final updatedLocation = await client.updateLiveLocation(
  messageId: liveLocationMessageId,
  createdByDeviceId: 'test_device_id', // Optional
  location: LocationCoordinates(
    latitude: newLatitude,
    longitude: newLongitude,
  ),
);

Whenever the location is updated, the message will automatically be updated with the new location.

The SDK will also notify your application when it should start or stop location tracking as well as when the active live location messages change.

// You can listen to the current user's active live locations in order to determine when to start
// or stop location tracking.
_activeLiveLocationsSubscription = _client.state.activeLiveLocationsStream
    .distinct((prev, curr) => prev.length == curr.length)
    .listen((locations) async {
  // If there are no more active locations to update, stop tracking.
  if (locations.isEmpty) return _stopTrackingLocation();
  // Otherwise, start tracking the user's location.
  return _startTrackingLocation();
});

Events

Whenever a location is created or updated, the following WebSocket events will be sent:

  • message.new: When a new location message is created.
  • message.updated: When a location message is updated.

In Dart, these events are resolved to more specific location events:

  • location.shared: When a new location message is created.
  • location.updated: When a location message is updated.

You can easily check if a message is a location message by checking the message.sharedLocation property. For example, you can use this events to render the locations in a map view.

// Listen for location-specific events
client.on(EventType.locationShared).listen((event) {
  // Handle location shared event if needed
});

client.on(EventType.locationUpdated).listen((event) {
  // Handle location updated event if needed
});
© Getstream.io, Inc. All Rights Reserved.