Skip to main content

Data Subscriptions & Live Updates

Any data query can be subscribed to. This replicates server data to the client in the exact shape defined by the query. The first delivery is a full data object. After that, when data changes on the server, a delta is delivered immediately to all subscribed clients.

info

Use subscriptions to keep a local copy of server data up to date via reactive deltas, with synchronisation traffic optimised using JSONPatch.


Create a subscription

A subscription is created on top of a query object.

var player = PlayServ.Model<Player>().Key(playerId);
var subscription = player.Listen();

Delivery model

The first message delivers the full object in the query shape.

When the server-side data changes, deltas are delivered to all listeners without delay. Changes are applied to the local data object automatically. If you need control over that process, subscribe to the Update event.


Observe updates explicitly

Use Wait<Update> on the subscription to handle updates when the data changes.

subscription.Wait<Update>(event => {
// Called whenever the data is updated
// event.new - the new version of the data
// event.old - the previous version of the data
});

Transport optimisation: JSONPatch

For traffic optimisation during synchronisation, PlayServ uses JSONPatch. JSONPatch describes changes that must be applied to a data object. Even though the database stores data as key=value, this approach remains effective.


Cancel a subscription

To cancel a subscription, call Stop().

subscription.Stop();

Subscription restoration after session desynchronisation

After a session desynchronisation — when the session enters Offline or Recovering and returns to Active — all subscriptions that were active at the moment of desynchronisation must be restored. This is handled automatically by the SDK.


Next step

Continue with Data Mutation & Model Extending to understand how to write data changes back to the server.

For related topics: