stream

The stream service returns the updates flowing through the platform, one message at a time, in the order in which they were published. Use it to keep a local copy of the data up to date.

  • To stream the updates of all entities in the namespace:

    https://consumer.qa.data.spglobal.com/namespace/stream?...
    
  • To stream the updates of one entity only:

    https://consumer.qa.data.spglobal.com/namespace/Entity/stream?...
    

Message structure

Every response is a JSON document, a stream message, similar to this UPDATE message:

{
  "cursor": 12125764,
  "at": "2017-01-01T00:00:00Z",
  "messageType": "UPDATE",
  "entityType": "Entity",
  "data": [...],
  "publishRequestId": "X0.1606987416.1",
  "timeToFirstWrite": 1,
  "after": "12125764,123"
}
FieldDescription
cursorThe position of the message in the stream. The cursor increases with each message but the values are not consecutive. The cursor applies to the whole namespace, so the cursors of /namespace/Entity/stream are a subset of those of /namespace/stream.
atThe time the message entered the stream. This is a technical field without business meaning.
messageTypeThe type of message. See below.
entityTypeThe entity the message applies to.
receivedSequenceFor datasets published in batches: the position at which the message was received. The cursor is then the position at which it was committed.
modeThe publication mode of the message, for example NORMAL or BACKFILL.
publishRequestIdThe identifier of the publication that produced the message.
batchRunIdFor messages with records: the batch run that published them, for datasets published in batches.
timeToFirstWriteThe time in milliseconds the server needed before it started to send the response.
afterThe value to use for the after parameter of the next request.

Important: The structure of messages evolves. New fields and new message types can be added at any time. A client must ignore fields it does not know, and must log and skip message types it does not know instead of failing.

Message types

Message typeDescription
UPDATENew or updated instances, in the data array.
DELETEThe instances with the identifiers in ids were deleted. When from, to and datetimeField are present, only the records within that time range of the field datetimeField are deleted. Otherwise all records of the entity with these ids are deleted.
BULK_UPDATEA change that was applied to many records at once (for datasets running in extended mode). Only returned with messageType=BULK_UPDATE. data is a list of objects with filter (the fields and values of the records that are changed), update (the new values), applyOnTimeseries and applyOnSnapshot.
DELETE_BY_KEY, RETENTION_DELETERecords that were deleted by key or removed by the retention policy. As UPDATE, data holds the deleted records.
PURGEData was removed completely from the platform, including from the stream. The from and to range and a filters list of filterField and filterValue pairs can describe which data. The identifiers of the removed records are not provided.
DICTIONARY_UPDATEThe data dictionary of the namespace changed, potentially adding fields or entities. data has the new version of the dictionary.
BATCH_RUN_EVENTMarks the start, the end or the cancellation of a batch run, with the fields eventType (START, END or CANCEL), batchId, batchRunId, asOf, recordCount and mode. In API version 2 the message types are BATCHRUN_START, BATCHRUN_COMPLETE and BATCHRUN_CANCEL. See API versions.
CLEAR_DATAThe API drops the data of the entity.
REFRESH_CACHEThe API refreshes its caches.
EMPTYA message without data. See below.

Examples of the other messages:

{
  "cursor": 89589,
  "at": "2017-01-01T00:00:00Z",
  "messageType": "DELETE",
  "entityType": "Entity",
  "ids": ["012345678911"],
  "from": "2017-01-01T23:00:00Z",
  "to": "2017-01-02T23:00:00Z",
  "datetimeField": "asOfDateTime",
  "publishRequestId": "X0.1606987416.1",
  "timeToFirstWrite": 1,
  "after": "89589"
}
{
  "cursor": 19101427,
  "at": "2017-06-29T08:27:09.991Z",
  "messageType": "EMPTY",
  "timeToFirstWrite": 1,
  "after": "19101427"
}

EMPTY messages

In API version 2 an EMPTY message has no messageType and no at field. An EMPTY message is returned in two situations:

  • The end of the stream is reached and there are no new updates. The after value equals the after value of the request.
  • No message matching your request and entitlements was found within a reasonable time. The after value differs from the request and marks how far the server has searched.

In both cases the correct response is to request the next message immediately with the returned after value. When nothing could be scanned at all, for example on an empty stream or when no message matches from and to, after is -1. Never use -1 in a request; check your parameters or timing instead.

Reading the stream

On every call after the first, set after to the after value of the previous response:

https://consumer.qa.data.spglobal.com/namespace/stream?after=AFTER_VALUE

Important: Request the next message immediately after you receive a response. Process the message in another thread so that processing does not delay polling.

Where to start is selected with the first request:

RequestResult
after=AFTER_VALUEThe message after that position. If there is none, an EMPTY message with the same after value.
after=headAn EMPTY message whose after value is the current end of the stream. Reading from there returns only new messages.
after=scanThe most recent message that contains data. If none is found within a certain period, an EMPTY message.
from=DATE without afterThe first message published at or after that date and time, followed by all later messages.
neitherThe default of your account, normally the same as after=head.

from and to refer to the time at which a message was published, not to a date field in the data.

Limiting the size of a response

limit restricts the number of records in a response. When an UPDATE message holds 1000 records and the limit is 200, the message is delivered in five responses. The after value of each response selects the next part, in the form cursor,index. The limit can be at most 10,000.

Combining messages

With aggregate=true and a limit up to 10,000, one response contains the data of several messages, which reduces the effect of network latency on streams with a high update rate. The response then has a messages array instead of data, and aggregated is true:

.../stream?aggregate=true&limit=10000

Filtering the stream

The stream can be filtered on properties of the message. These filters are indexed and can reduce the volume of the stream considerably.

ParameterDescription
messageTypeOnly messages of this type. Use the names of API version 2 as listed in API versions, such as UPDATE, DELETE, BULK_UPDATE and BATCHRUN_COMPLETE. EMPTY and BATCH_RUN_EVENT are not accepted.
modeOnly messages with this publication mode, for example NORMAL, BACKFILL, RESTATEMENT or ADJUSTMENT.
batchRunOnly messages of this batch run.
receivedSequenceOnly messages with this received sequence. This links records from the latest and timeseries services (with showMessage) to the stream.
entityOn the stream of a namespace: only these entities.
Entity.fieldOn the stream of a namespace: only records of the entity with this value of the field, for example Price.isin=US0378331005. Messages that carry no records, such as DELETE, are not returned when you use these filters.
id and other filtersOn the stream of an entity: only the records that match. Messages without matching records are returned as EMPTY.
fieldsRestricts the fields in data.
returnPreSignedUrlReplaces attachment ids by download URLs.

Waiting for new messages

Use wait to have the server hold a request at the end of the stream until a message arrives or the time is over. The default is 1000 ms and the maximum is 300,000 ms.

Retrieving a single message

cursor returns one specific message again.

Sharing the stream

modulus and part divide the stream over parallel requests.