Comparison

Comparing usage of off-chain vs on-chain reactivity

Somnia Reactivity supports two subscription modes: off-chain (via WebSocket in TypeScript) for flexible, external app integration, and on-chain (via Solidity handlers) for automated, trustless blockchain reactions. Choose based on your dApp's needs—off-chain for UIs/backends, on-chain for DeFi/automation.

Comparison Table

Aspect
Off-Chain (WebSocket/TypeScript)
On-Chain (Solidity/EVM)

Delivery Mechanism

WebSocket push to your app/server

Direct EVM invocation of handler contract

Execution Environment

Off-chain (Node.js, browser)

On-chain (Somnia validators execute)

Gas / Costs

None per notification (pay for node or rpc provider)

Pays gas per invocation (from min 32 SOM balance)

Latency

Near-real-time (block time + network)

Block time (atomic with chain state)

State Access

Include ETH view calls in sub; flexible queries

Limited to handler logic; no external calls;Solidity contract does its own external view calls

Use Cases

Front-ends (live UIs), backends (DB updates), integrations

DeFi (auto-compound), NFTs (reactive mints), oracles

Reliability Options

Can specify callback only executed when state changes

Can specify if you want delivery regardless if block inclusion distance > 0, can also coalece

Setup Complexity

Install SDK, subscribe callback

Deploy handler contract, create/fund sub

Security

App-level (e.g., auth WebSockets)

Blockchain-level (reentrancy risks in handlers)

Scalability

Handles high volume off-chain

Limited by gas/block; use coalescing for batches

When to Use Off-Chain Subscriptions

  • Pros: No gas per event, easy integration with web apps, full access to external data/APIs.

  • Cons: Relies on your app being online

  • Example: Real-time dashboard updating on Transfer events without chain writes.

When to Use On-Chain Subscriptions

  • Pros: Fully decentralized reactions; executes automatically on-chain.

  • Cons: Gas costs accumulate; potential for loops if handlers emit events.

  • Example: Smart contract that auto-swaps tokens on price oracle updates.

Hybrid Approaches

Combine both: Use off-chain for monitoring/UI, trigger on-chain actions via transactions when needed.

Last updated