Subscriptions
This is a general introduction to the concept of subscriptions. For concrete implementations and code samples, have a look at the relevant client libraries
Subscriptions are at the very heart of Hivekit. They provide real-time update feeds for various concepts, such as realms, areas, instructions, and - most frequently - objects.
You can subscribe to changes by calling .subscribe()
on the relevant namespace, such as subscription = await realm.object.subscribe()
, await realm.instruction.subscribe()
, or await client.realm.subscribe()
, among others.
All .subscribe()
methods return a subscription object, which allows you to listen to incoming data, cancel and update the subscription.
Specifying Subscription Criteria
You can customize your subscription by specifying the criteria you're interested in. Here are some methods for specifying your subscription:
-
Attribute Filters: Use attribute filters such as
'type="deliveryRider"'
or'charge>0.3'
to subscribe to objects based on their attributes. -
Geographical Bounds: Subscribe to objects within specified geographical bounds, such as a circle, rectangle, or polygon.
-
Area-based Subscriptions: Subscribe to objects within an existing area, allowing you to receive updates for objects within a predefined geographical region.
-
Roaming Geofence Subscriptions: Subscribe to objects near a moving target, creating a dynamic geofence around a specific object and receiving notifications when objects enter or leave the geofence.
Updating Subscriptions
Hivekit allows you to update a subscription frequently. This is helpful for implementing features such as panning on a map, polygonal roaming geofences, or real-time filters for results.
Considerations when using Subscriptions
Sending large amounts of unneeded data to users increases your Hivekit cost, reduces the user's battery life, drains their internet data, and can impact your app's performance. To optimize your subscription, consider the following:
- Limit your subscription to what's necessary, using the tools mentioned above.
- Update the subscription criteria as needed, rather than creating multiple subscriptions.
- Cancel subscriptions when they are no longer needed.
By following these best practices, you can provide a smooth and efficient experience for your users while minimizing the impact on your app's resources and costs.