Ever Platform
RoadmapIdeasDeveloper ToolsSDK
  • About Ever Platform
  • Sandbox endpoint
  • Quick Start
  • Use cases
    • Infrastructure Provider
      • Projects
    • Community-driven development
      • Roadmap
        • Discussions
      • Your ideas
      • DevNet Giver(Faucet)
    • Custom development
      • Our clients
  • Products
    • Evercloud
      • Networks Endpoints
      • Get Started
      • GraphQL API documentation
      • Testnet Faucets
      • About data proofs in Cloud
      • SLA
      • HTTP notifications
    • Dedicated Cloud/Node
    • Dapp Server (DS)
    • Simple Emulator (SE)
      • Endpoint
    • Network Emulator (NE)
    • Functionality comparison
  • Samples
    • GraphQL Samples
      • Send message
      • Network Config
      • Blocks
      • Accounts
      • Transactions
      • Messages
      • Block and Transaction Pagination: Best Practice
      • Multiple Message Processing and Monitoring
      • Subscribe for REMP receipts
  • Reference
    • GraphQL API
      • Quick Start
      • Samples
      • Networks
      • Explore Playground
      • Connect to GraphQL API
      • Schema
      • Blockchain API
      • Info API
      • Query Collections: Query Language
      • Subscribe Collections
      • Message Monitor API
      • Field Descriptions
    • Evercloud API Add-ons
      • Query cost
      • Blockchain Statistics
      • FT (Fungible Token) API
      • Price
      • Counterparties
      • Flex API
    • Changelog
      • Evercloud
      • Dapp Server (DS)
      • Simple Emulator (SE)
    • Breaking changes
      • Policy
      • Deprecation schedule
      • Migration guides
        • GraphQL API 1.0 migration
  • SDK and tools
    • everdev
    • Client Libraries
  • Guides
    • How to connect to GraphQL API
  • 😊Social
    • Feedback page
    • Telegram
    • Discord
Powered by GitBook
On this page

Was this helpful?

  1. Reference
  2. GraphQL API

Subscribe Collections

Subscribe to blockchain events

PreviousQuery Collections: Query LanguageNextMessage Monitor API

Last updated 1 year ago

Was this helpful?

You can create a websocket subscription for any event in the blockchain.

Read filter syntax and collections documentation .

In this example, we start a subscription and get a result whenever a new block is generated.

subscription {
  blocks {
    id
  }
}

The filter and result parameters are the same as in the query method. The filter parameter narrows the action down to a subset of monitored items. In this case, the filter is empty: all items are included into monitoring.

wscat -c your-wss-endpoint -s graphql-ws
{"id":"1","type":"start","payload":{"variables":{},"extensions":{},"operationName":null,"query":"subscription{\n  blocks(filter:{\n    workchain_id:{\n      eq:-1\n    }\n  }){\n    seq_no\n    id\n  }\n}"}}
const {TonClient} = require('@eversdk/core');
const {libNode} = require('@eversdk/lib-node');

TonClient.useBinaryLibrary(libNode)

const client = new TonClient({
  network: {
    endpoints: [
      process.env.endpoint
    ],
    queries_protocol: 'WS'
  },
});


function responseHandler(data, responseType) {
  // Tip: Always wrap the logic inside responseHandler in a try-catch block
  // or you will be surprised by non-informative errors due to the context
  // in which the handler is executed
  try {
    if (responseType === 100 /* GraphQL data received */) {
      if (data?.result?.blocks) {
        console.log(data.result.blocks);
      }

    } else {
      // See full list of error codes here:
      // https://docs.everos.dev/ever-sdk/reference/types-and-methods/mod_net#neterrorcode
      console.error(data, responseType);
    }
  } catch (err) {
    console.log(err);
  }
}

(async () => {
    try {
      const subscription = /* language=graphql */ `subscription($wc: Int!) {
          blocks(filter: { workchain_id: { eq: $wc } }) {
              seq_no
              id
          }
      }`
      await client.net.subscribe({
        subscription,
        variables: {wc: -1},
      }, responseHandler)
      console.log('The last masterchain blocks')
      console.log('Press CTRL+C to interrupt it')
    } catch (error) {
      if (error.code === 504) {
        console.error('Network is inaccessible');
      } else {
        console.error(error);
      }
      process.exit(1);
    }
  }
)();
URL: wss://mainnet.evercloud.dev/your-project-id/graphql 
Sec-WebSocket-Protocol: graphql-ws

message
{
  "id": "1",
  "type": "start",
  "payload": {
    "variables": {},
    "extensions": {},
    "operationName": null,
    "query": "subscription{\n  blocks(filter:{\n    workchain_id:{\n      eq:-1\n    }\n  }){\n    seq_no\n    id\n  }\n}"
  }
}
in the previous section