LLM Notice: This documentation site supports content negotiation for AI agents. Request any page with Accept: text/markdown or Accept: text/plain header to receive Markdown instead of HTML. Alternatively, append ?format=md to any URL. All markdown files are available at /md/ prefix paths. For all content in one file, visit /llms-full.txt
Skip to main content

events

Subscribes to Flow blockchain events in real-time. This function provides a way to listen for specific events emitted by smart contracts on the Flow blockchain. It automatically handles fallback to legacy polling for environments that don't support WebSocket subscriptions.

Import​

You can import the entire package and access the function:


_10
import * as fcl from "@onflow/fcl"
_10
_10
fcl.events(filterOrType)

Or import directly the specific function:


_10
import { events } from "@onflow/fcl"
_10
_10
events(filterOrType)

Usage​


_37
// Subscribe to a specific event type
_37
import * as fcl from "@onflow/fcl"
_37
_37
const unsubscribe = fcl.events("A.0x1654653399040a61.FlowToken.TokensWithdrawn")
_37
.subscribe((event) => {
_37
console.log("Event received:", event)
_37
console.log("Event data:", event.data)
_37
console.log("Transaction ID:", event.transactionId)
_37
})
_37
_37
// Stop listening after 30 seconds
_37
setTimeout(() => {
_37
unsubscribe()
_37
}, 30000)
_37
_37
// Subscribe to multiple event types with error handling
_37
const unsubscribe = fcl.events({
_37
eventTypes: [
_37
"A.0x1654653399040a61.FlowToken.TokensWithdrawn",
_37
"A.0x1654653399040a61.FlowToken.TokensDeposited"
_37
]
_37
}).subscribe(
_37
(event) => {
_37
console.log("Token event:", event.type, event.data)
_37
},
_37
(error) => {
_37
console.error("Event subscription error:", error)
_37
}
_37
)
_37
_37
// Subscribe to events starting from a specific block height
_37
const unsubscribe = fcl.events({
_37
eventTypes: ["A.CONTRACT.EVENT"],
_37
startBlockHeight: 12345678
_37
}).subscribe((event) => {
_37
console.log("Historical and new events:", event)
_37
})

Parameters​

filterOrType (optional)​

  • Type: string | EventFilter
  • Description: Event filter object or event type string. If a string is provided, it will be treated as a single event type to subscribe to. If an EventFilter object is provided, it can contain multiple event types and other filter criteria.

Properties:​

  • eventTypes - Array of event type strings to subscribe to
  • startBlockId - Block ID to start streaming from
  • startBlockHeight - Block height to start streaming from

Returns​


_10
{
_10
subscribe: (onData: (event: Event) => void, onError?: (error: Error) => void) => () => void;
_10
}

An object containing a subscribe method • returns.subscribe Function to start the subscription • returns.subscribe.onData Callback function called when an event is received • returns.subscribe.onError Optional callback function called when an error occurs • returns.subscribe.unsubscribe Function returned by subscribe() to stop the subscription


Rate this page