PrivMX DOCS
Java

Handling Events

How to subscribe to events, assuming you have already started listening.

It’s recommended to register event callbacks using the registerManyCallbacks() method. This allows to reduce the number of requests sent to the server.

For most events, you need to set the eventSelectorType option to control the scope of the registration. This allows you to define whether the event should be registered for the entire context, a specific container, or a single item.

Additionally, you can combine registrations from different APIs and send them together using registerManyCallbacks() method, making the process even more efficient.

val newUserCallbacksGroup = "NEW_USER_CALLBACKS_GROUP"
val newMessageCallbacksGroup = "NEW_MESSAGE_CALLBACKS_GROUP"
val threadID = "THREAD_ID"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        newUserCallbacksGroup,
        EventType.ContextUserAddedEvent(contextId)
    ) { newUserData ->
        // e.g. Send a message to a new user who has been added to the context
    },

    CallbackRegistration(
        newMessageCallbacksGroup,
        EventType.ThreadNewMessageEvent(
            ThreadEventSelectorType.THREAD_ID,
            threadID
        )
    ) { newMessageData ->
        // e.g. Notify me when new message is posted in this thread
    }
)

Core

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.ContextUserAddedEvent(contextId)
    ) { newUserData ->
        // some actions when a user is added to the context
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.ContextUserRemovedEvent(contextId)
    ) { removedUserData ->
        // some actions when a user is removed from the context
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.ContextUsersStatusChangeEvent(contextId)
    ) { usersWithStatusUpdateData ->
        // some actions when user statuses have changed
    }
)

Connection

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.ConnectedEvent
    ) {
        // some actions when lib was connected
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.DisconnectedEvent
    ) {
        // some actions when lib was disconnected
    }
)

Threads

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.ThreadCreatedEvent(contextId)
    ) { newThreadData ->
        // some actions when a new thread is created
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.ThreadUpdatedEvent(
            ThreadEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { threadUpdateData ->
        // some actions when a thread is updated
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.ThreadStatsChangedEvent(
            ThreadEventSelectorType.CONTEXT_ID,
            contextId,
        )
    ) { threadUpdateData ->
        // some actions when thread stats have changed
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.CollectionChangedEvent(
            ThreadEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { changedCollectionData ->
        // some actions when thread collection changes
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.ThreadDeletedEvent(
            ThreadEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { deletedThreadData ->
        // some actions when thread is deleted
    }
)

Read more about Thread events.

Stores

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.StoreCreatedEvent(contextId)
    ) { newStoreData ->
        // some actions when new store created
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.StoreUpdatedEvent(
            StoreEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { storeUpdateData ->
        // some actions when a store is updated
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.StoreStatsChangedEvent(
            StoreEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { storeStatsUpdateData ->
        // some actions when store stats have changed
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.CollectionChangedEvent(
            StoreEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { changedCollectionData ->
        // some actions when store collection changes
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.StoreDeletedEvent(
            StoreEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { deletedStoreData ->
        // some actions when a store is deleted
    }
)

Read more about Store events.

Inboxes

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.InboxCreatedEvent(contextId)
    ) { newInboxData ->
        // some actions when a new inbox is created
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.InboxUpdatedEvent(
            InboxEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { inboxUpdateData ->
        // some actions when an inbox is updated
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.CollectionChangedEvent(
            InboxEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { changedCollectionData ->
        // some actions when inbox collection changes
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.InboxDeletedEvent(
            InboxEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { deletedInboxData ->
        // some actions when an inbox is deleted
    }
)

Read more about Inbox events.

KVDBs

val callbacksGroup = "CALLBACKS_GROUP"

endpointSession.registerManyCallbacks(
    CallbackRegistration(
        callbacksGroup,
        EventType.KvdbCreatedEvent(contextId)
    ) { kvdbCreatedData ->
        // some actions when a new KVDB is created
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.KvdbUpdatedEvent(
            KvdbEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { kvdbUpdatedData ->
        // some actions when a KVDB is updated
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.KvdbStatsChangedEvent(
            KvdbEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { kvdbStatsUpdateData ->
        // some actions when kvdb stats have changed
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.CollectionChangedEvent(
            KvdbEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { changedCollectionData ->
        // some actions when kvdb collection changes
    },

    CallbackRegistration(
        callbacksGroup,
        EventType.KvdbDeletedEvent(
            KvdbEventSelectorType.CONTEXT_ID,
            contextId
        )
    ) { kvdbDeletedData ->
        // some actions when KVDB deleted
    }
)

Read more about KVDB events.

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page