Page History
Table of Contents | ||
---|---|---|
|
...
The subscription API provides access to the transaction log, in chronological order, for bulk and real-time subscription service for external client applicationsstorage unit updates.
Subscribe
Provides a subscription service to the transaction collection log of a storage unit. The service will automatically push new transactions and the records linked with each transaction records to the subscriber clients. The subscription service uses Server-Sent EventsEvents protocol for communication.
Reconnection: If the connection is lost by the client, when reconnecting it will send a last-event-id header with the last transaction id that was received by the client. The subscription service will resume from the last-event-id.
...
The subscribe GET request receives the storage unit name and optionally the scope to subscribe to . Optionally a start transaction id can be provided, the server will send transactions starting on this transaction id. If the start id is not provided, all transactions are sent.in the URL. It can also received a startId parameter to start getting transactions starting on that particular one, a doContentProcessing (default true) boolean parameter to trigger or not the Fetch events from content processing modules, a transactionsOnly (default false) boolean parameter to indicate whether or not the record data should be sent along a add/update transaction and a zone parameter to indicate from which zone to get transactions from (all if omitted).
Code Block | ||||
---|---|---|---|---|
| ||||
GET subscription/subscribe/<storage-unit>/<optional-scope>?startId=5644f2d497cc11144081d111&doContentProcessing=true&transactionsOnly=false&zone=LOCAL_ZONE |
Java example
Code Block | ||||
---|---|---|---|---|
| ||||
Client client = ClientBuilder.newBuilder().register(SseFeature.class).build(); WebTarget target = client.target(new URI("http://localhost:3000/subscription/subscribe/<storage-unit-name>/<scope>")); EventSource eventSource = EventSource.target(target).reconnectingEvery(2, TimeUnit.SECONDS).build(); EventListener listener = new EventListener(){ public void onEvent(InboundEvent inboundEvent) { System.out.println(inboundEvent.getId() + "; " + inboundEvent.readData(String.class).length()); } }; eventSource.register(listener); eventSource.open(); ... eventSource.close(); |
...
Code Block | ||||
---|---|---|---|---|
| ||||
var EventSource = require('eventsource') // https://www.npmjs.com/package/eventsource var source = new EventSource('http://localhost:3000/subscription/subscriptionEvent/<storage-unit-name>/<scope>'); source.onmessage = function (e) { console.log(e.lastEventId + ':' + e.data.length); }; |
Response
The response is of of type text/event-stream Content-type and and remains open until the client decides to unsubscribe. Each transaction record log entry is returned as a single event with the transaction id as the event id and the transaction JSON as the data. If the transaction has a content record associated, a second JSON object is sent with the record data as a second data entry on the response, separated from the previous with a single new line. Each new event message is separated by two new lines (\n\n).
Code Block | ||||
---|---|---|---|---|
| ||||
id: 5644f2d497cc11144081d111\n data: {"_id" : 5644f2d497cc11144081d111, "metadata" : {...}, "scope" : "ocrconnector", "recordId" : "LOCAL_ZONE:E0F57D337D5021236E353C7AB305F147", "timestamp" : 2015-11-12T20:13:08.243Z, "type" : "ADD", "__v" : 0}\n data:{"key": "LOCAL_ZONE:E0F57D337D5021236E353C7AB305F147", "content": {"connector": {"doc": {"test": "data"}}}}\n\n |