Managing Internal State Example
In this example, we are creating an app that records the number of messages sent on the server using the Persistence object.
To store the message count, we can write an executePostMessageSent
event handler as shown below:
Here,
The internal state is
count
but not the count variable whose data is stored in the memory. We use the temporary variablecount
to store the number of messages sent and retrieve this number from the persistence storage.Every time the handler
executePostMessageSent
is called, we increase the count by one and then store it back to the persistence storage.
In this way, even in a cluster environment, your app in each Rocket.Chat instance can share data from a single data source, the Rocket.Chat persistence storage and maintain data consistency.
Last updated