GetMessages

Usage

Import the Any declaration to interact with the GetMessages function to retrieve the messages of your choice.

import { messages } from 'aleph-sdk-ts';

// Or used optimized import
import { GetMessages } from 'aleph-sdk-ts/dist/messages/any';

Then call the GetMessages function. Notice this function can be called without providing any parameters. It will returns by default the 20 lastest messages posted on the network.

(async () => {
    const res = await GetMessages({});
})();

Parameters

ParameterDescription

pagination - optional number

Number of messages per page.

page - optional number

The page index to show.

addresses - optional string[]

Array of addresses referenced in attribute sender of messages.

channels - optional string[]

Array of channels.

chains - optional Chain[]

Array of chains. Note with this params the call to the API can take a certain time.

refs - optional string[]

Arrays of refs listed in messages.

tags - optional string[]

Array of tags. This params is dedicated to Post Message.

contentTypes - optional string[]

Array of content types to find. This params is dedicated for Post Message.

contentKeys - optional string[]

Array of content keys. This params is dedicated for Aggregate Message.

hashes - optional string[]

List of Hashes to resolve.

messageType - optional MessageType

The type of message you are waiting for: post, aggregate, forget, store, program.

StartDate - optional Date

Use the time attribute to filter messages. Only message wicht passed this date will be retrieved.

EndDate - optional Date

Use the time attribute to filter messages. Only message wicht anterior to this date will be retrieved.

APIServer - optional string

The APIServer to use to resolve the request. The default one is: https://api2.aleph.im

Result

GetMessages will returns a Promise response with this content:

type MessageQueryResponse = {
    messages: BaseMessage[]; // List of fetched messages
    pagination_page: number; // The current page fetched
    pagination_total: number; // The total number of messages according your parameters
    pagination_per_page: number; // Number of messages per page
    pagination_item: string; // type of content fetched, usualy 'messages'
};

You have 2 choices to get along with fetched data:

A. Keep default

By default, the GetMessages method will return a response like this:

This will allow you to get with auto-completion generic information about the messages like the sender, date, chain, ... that does not depend on its type (Post, Store, Aggregate, ...).

(async () => {
    const res = await GetMessage({
        hash: "87e1e2ee2cbe88fa2923042b84b2f9c69410005ca7dd40193838bf9bad18e12c",
    });

    if (res.messages.length > 0)
        res.messages[0].//sender,chain,signature,size,time,hash_type,item_hash,type,...
})();

B. Use Typeguards

This is the safest way to handle messages retrieved by GetMessage or GetMessages. You can find more informaton about them on their dedicated page.

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await any.GetMessages({});
    const firstMessage = res.messages[0];

    if (any.is.Store(firstMessage))
        firstMessage.content.//item_hash,... the first item is now of type StoreMessage.
})();

Last updated