Managing Internal State Example
    • Dark
      Light
    • PDF

    Managing Internal State Example

    • Dark
      Light
    • PDF

    Article summary

    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:

    public async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
    //create a new association to count messages
        const association = new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message-count');
        const persis = read.getPersistenceReader();
        
        try {
            let count = 0;
            const record = await persis.readByAssociation(association);
            if (record?.count) count = record.count;
    
            await persistence.createWithAssociation({ count: ++count || 0 }, association);
    //log the number of messages
            console.log(`Message Sent Count: ${ count }`)
        } catch (err) {
            return this.getLogger().error(err);
        }
    }

    Here,

    • The internal state is count but not the count variable whose data is stored in the memory. We use the temporary variable count 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.

    Check the full demo here for more details!


    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.
    ESC

    Eddy AI, facilitating knowledge discovery through conversational intelligence