Skip to content

The subscribeEntityEvents method on the public client lets you listen for entity changes in real time. It polls the chain for new events and fires callbacks when entities are created, updated, deleted, expired, or have their expiration extended.

import { createPublicClient, http } from "@arkiv-network/sdk"
import { kaolin } from "@arkiv-network/sdk/chains"
const client = createPublicClient({
chain: kaolin,
transport: http(),
})
const unsubscribe = await client.subscribeEntityEvents({
onEntityCreated: (event) => {
console.log("Entity created:", event.entityKey)
},
onEntityUpdated: (event) => {
console.log("Entity updated:", event.entityKey)
},
onEntityDeleted: (event) => {
console.log("Entity deleted:", event.entityKey)
},
onEntityExpired: (event) => {
console.log("Entity expired:", event.entityKey)
},
onEntityExpiresInExtended: (event) => {
console.log("Entity extended:", event.entityKey)
},
onError: (error) => {
console.error("Subscription error:", error)
},
})
// Later, stop listening:
unsubscribe()

subscribeEntityEvents takes the event handlers object, plus two optional arguments:

ParameterTypeDescription
Event handlersobjectObject with callback functions (see below)
pollingIntervalnumberPolling interval in milliseconds (optional)
fromBlockbigintBlock number to start listening from (optional)
HandlerEvent TypeFires When
onEntityCreatedOnEntityCreatedEventA new entity is created
onEntityUpdatedOnEntityUpdatedEventAn entity’s payload or attributes change
onEntityDeletedOnEntityDeletedEventAn entity is deleted by its owner
onEntityExpiredOnEntityExpiredEventAn entity’s expiration time is reached
onEntityExpiresInExtendedOnEntityExpiresInExtendedEventAn entity’s expiration is extended
onErrorErrorAn error occurs during polling
{
entityKey: Hex // Key of the new entity
owner: Hex // Address of the entity owner
expirationBlock: number // Block when the entity expires
cost: bigint // Storage cost of the operation
}
{
entityKey: Hex // Key of the updated entity
owner: Hex // Address of the entity owner
oldExpirationBlock: number // Previous expiration block
newExpirationBlock: number // New expiration block
cost: bigint // Storage cost of the operation
}
{
entityKey: Hex // Key of the deleted entity
owner: Hex // Address of the entity owner
}
{
entityKey: Hex // Key of the expired entity
owner: Hex // Address of the entity owner
}
{
entityKey: Hex // Key of the extended entity
owner: Hex // Address of the entity owner
oldExpirationBlock: number // Previous expiration block
newExpirationBlock: number // New expiration block
cost: bigint // Storage cost of the operation
}

Use fromBlock to replay events starting from a past block:

const unsubscribe = await client.subscribeEntityEvents(
{
onEntityCreated: (event) => {
console.log("Created:", event.entityKey, "at cost:", event.cost)
},
},
5000, // poll every 5 seconds
100n, // start from block 100
)

Listen for changes to your own entities and log a summary:

import { createPublicClient, http } from "@arkiv-network/sdk"
import { kaolin } from "@arkiv-network/sdk/chains"
const client = createPublicClient({
chain: kaolin,
transport: http(),
})
const MY_ADDRESS = "0xYourAddress..."
const unsubscribe = await client.subscribeEntityEvents({
onEntityCreated: (event) => {
if (event.owner === MY_ADDRESS) {
console.log(`New entity ${event.entityKey} expires at block ${event.expirationBlock}`)
}
},
onEntityDeleted: (event) => {
if (event.owner === MY_ADDRESS) {
console.log(`Entity ${event.entityKey} was deleted`)
}
},
onEntityExpired: (event) => {
if (event.owner === MY_ADDRESS) {
console.log(`Entity ${event.entityKey} has expired`)
}
},
onError: (error) => {
console.error("Event stream error:", error)
},
})
// Clean up on exit
process.on("SIGINT", () => {
unsubscribe()
process.exit()
})