Imagine an application that displays real time information about the position and status of a fleet of delivery vans. When the application starts, it should display the current location of each of the vans along with their current status. As vans move around the city and post other status updates, the application should keep its display up to date. Vans upload information to the system by posting messages to the van_location
topic, configured with a key of van_id
on the AMPS server.
In this application, it is important to not only stay up-to-date on the latest information about each van, but to ensure all of the active vans are displayed as soon as the application starts. Combining a SOW with a subscription to the topic is exactly what is needed, and that is accomplished by the Client.sowAndSubscribe()
method, or by executing a sow_and_subscribe
command.
First, let's look at an example that uses the convenience method:
Now we will look at an example that uses the Command
interface with the Client.execute()
method:
Notice that the two forms have the same result.
In the above examples we specified the oof
option to the command. Setting this option causes AMPS to send Out-of-Focus (OOF) messages for the topic. OOF messages are sent when an entry that was sent to us in the past no longer matches our query. This happens when an entry is removed from the SOW cache via a sow_delete
operation, when the entry expires (as specified by the expiration time on the message or by the configuration of that topic on the AMPS server), or when the entry no longer matches the content filter specified. In our case, if a van's status changes to something other than ACTIVE, it no longer matches the content filter, and becomes out of focus. When this occurs, a message is sent with Command
set to oof
. We use OOF messages to remove vans from the display as they become inactive, expire, or are deleted.
AMPS State of the World (SOW) allows you to automatically keep and query the latest information about a topic on the AMPS server, without building a separate database. Using SOW lets you build impressively high-performance applications that provide rich experiences to users. The AMPS JavaScript client lets you query SOW topics and subscribe to changes with ease.
To begin, we will look at a simple example of issuing a SOW query.
In the example above, we invoke Client.sow()
to initiate a SOW query on the orders
topic, for all entries that have a symbol of 'ROL'
. As usual, the Client.sow()
method returns a Promise
object that resolves with the query ID.
As the query executes, the message handler function is invoked for each matching entry in the topic. Messages containing the data of matching entries have a Command
of value sow
, so as those arrive, we write them to the console.
The AMPS clients include a batch size parameter that specifies how many messages the AMPS server will return to the client in a single batch when returning the results of a SOW query. The 60East clients set a batch size of 10 by default. This batch size works well for common message sizes and network configurations.
Adjusting the batch size may produce better network utilization and produce better performance overall for the application. The larger the batch size, the more messages AMPS will send to the network layer at a time. This can result in fewer packets being sent, and therefore less overhead in the network layer. The effect on performance is generally most noticeable for small messages, where setting a larger batch size will allow several messages to fit into a single packet. For larger messages, a batch size may still improve performance, but the improvement is less noticeable.
In general, 60East recommends setting a batch size that is large enough to produce few partially-filled packets. Bear in mind that AMPS holds the messages in memory while batching them, and the client must also hold the messages in memory while receiving the messages. Using batch sizes that require large amounts of memory for these operations can reduce overall application performance, even if network utilization is good.
For smaller message sizes, 60East recommends using the default batch size, and experimenting with tuning the batch size if performance improvements are necessary. For relatively large messages (especially messages with sizes over 1MB), 60East recommends explicitly setting a batch size of 1 as an initial value, and increasing the batch size only if performance testing with a larger batch size shows improved network utilization or faster overall performance.
AMPS allows applications to manage the contents of the SOW by explicitly deleting messages that are no longer relevant. For example, if a particular delivery van is retired from service, the application can remove the record for the van by deleting the record for the van.
The client provides the following methods for deleting records from the SOW:
sowDelete()
- Accepts a topic and filter, and deletes all messages that match the filter from the topic specified.
sowDeleteByKeys()
- Accepts a set of SOW keys as a comma-delimited string and deletes messages for those keys, regardless of the contents of the messages. SOW keys are provided in the header of a SOW message, and are the internal identifier AMPS uses for that SOW message.
sowDeleteByData()
- Accepts a topic and message, and deletes the SOW record that would be updated by that message.
Most applications use sowDelete()
, since this is the most useful and flexible method for removing items from the SOW. In some cases, particularly when working with extremely large SOW databases, sowDeleteByKeys()
can provide better performance.
In either case, AMPS sends an OOF message to all subscribers who have received updates for the messages removed, as described in the previous section.
sowDelete()
returns a Promise that resolves with a Message
object. This Message
is an acknowledgment that contains information on the delete command. For example, the following snippet simply prints informational text with the number of messages deleted:
Acknowledging messages from a queue uses a form of the sow_delete
command that is only supported for queues. Acknowledgment is discussed in the Using Queues chapter in this guide.