# Query/Subscribe for messages(events)

How to work with contract events

* [About messages](#about-messages)
* [Query/subscribe to messages](#query-subscribe-to-messages)
* [Usage](#usage)
  * [Query](#query)
  * [Subscribe](#subscribe)
  * [Decode](#decode)

## About messages

Account messages are of 3 types:

* External inbound (`msg_type`=1 or `ExtIn` ). It is for example, deploy message or run message.
* Internal message(`msg_type`=0). It is when one contract calls another contract it sends an internal message. These messages are constructed with target contract ABI. Can be of 2 subtypes:
  * Internal inbound (`IntIn`)
  * Internal outbound (`IntOut`)
* External outbound messages(`msg_type`=2 or `ExtOut`). These can be of 2 subtypes:
  * Events - these are also specified in contract ABI. [Read more about working with events here](/ever-sdk/guides/work_with_contracts/work_with_events.md).
  * Return - these are generated by contract function's return.

## Query/subscribe to messages

You can fetch events of you contract like this:

```graphql
query{
  blockchain{
    account(address:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"){
      messages(msg_type:[ExtOut, ExtIn, IntIn, IntOut],first:2){
        edges{
          node{
            hash
            body
            created_at_string
          }
          cursor
        }
        pageInfo{
          hasNextPage
        }
      }
    }
  }
}
```

Or subscribe to them:

```graphql
subscription{
messages(
      filter:{ 
      src:{
        eq:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"
      }
      msg_type:{
        in:[0,1,2]
      }
    }
)
{
   id
  body
}
}
```

## Usage

### Query

See the full sample here <https://github.com/everx-labs/sdk-samples/tree/master/core-examples/node-js/pagination>

Read about used API here -> [Account messages pagination](https://docs.everplatform.dev/samples/graphql-samples/accounts#get-messages-within-block-range).

```javascript
result = await client.net.query({
  query: `query MyQuery($address: String!, $cursor: String, $count: Int, $start_seq_no: Int, end_seq_no: Int) {
    blockchain {
        account(address: $address){
            messages(
                master_seq_no_range: { start: $start_seq_no, end: $end_seq_no }
                first: $count,
                msg_type: [ExtOut, ExtIn, IntIn, IntOut],
                after: $cursor
            ) {
                edges {
                    node { id, created_at }
                }
                pageInfo {
                    endCursor
                    hasNextPage
                }
            }
        }
    }
}`,
       variables:{address, cursor, count, start_seq_no, end_seq_no}
}); 
```

### Subscribe

See the full sample here <https://github.com/everx-labs/sdk-samples/tree/master/core-examples/node-js/subscribe-and-decode>

To subscribe to all this. Don't forget to specify your own callback.

```javascript
const messageSubscription = await TonClient.default.net.subscribe_collection({
    collection: "messages",
    filter: {
        dst: { eq: your-contract-address },
        msg_type:{ in: [0,1,2] }
    },
    result: "boc"
}, <callback function>
});
```

### Decode

See the full sample here <https://github.com/everx-labs/sdk-samples/tree/master/core-examples/node-js/subscribe-and-decode>

```javascript
const decoded = (await TonClient.default.abi.decode_message({
                    abi: abiContract(your-contract-abi),
                    message: message_boc,
                }));
switch (decoded.body_type) {
                case MessageBodyType.Input:
                    console.log(`External inbound message, function "${decoded.name}", parameters: `, JSON.stringify(decoded.value));
                    break;
                case MessageBodyType.Output:
                    console.log(`External outbound message, function "${decoded.name}", result`, JSON.stringify(decoded.value));
                    break;
                case MessageBodyType.Event:
                    console.log(`External outbound message, event "${decoded.name}", parameters`, JSON.stringify(decoded.value));
                    break;
                }
```

Check out [AppKit documentation](https://docs.everos.dev/appkit-js/guides/work_with_events) for this use case.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.everos.dev/ever-sdk/guides/work_with_contracts/work_with_events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
