Skip to main content

Using the Momento Topics (pub/sub) API with Momento Cache

Momento Topics is a messaging pattern that allows for real-time communication between parts of a distributed application. It enables you to publish (produce) values to a topic, as well as subscribe (consume) from a topic. This page details the Momento API methods for interacting with Momento Topics.

For in-depth information, see Momento Topics.

Topics methods

Subscribe

This method subscribes to a topic to receive new values with a stateful connection.

NameTypeDescription
cacheNameStringName of the cache where the topic exists.
topicNameStringName of the topic to subscribe to.
This is example code.
Method response object
  • Success - Returns a subscription object.
  • Error

See response objects for specific information.

With the returned subscription object, once put in a for loop, your code will receive an event when a new value is published to the Topic.

Publish

Publishes a message to a topic.

NameTypeDescription
cacheNameStringName of the cache where the topic exists.
topicNameStringName of the topic to publish the value to.
valueString / bytesValue to publish to the topic.
This is example code.
Method response object
  • Success
  • Error

See response objects for specific information.

TopicClient

Instead of the CacheClient, as used in most Momento Cache API calls, for Topics you use a TopicClient object.