Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

  • NIMChatroomMessageInterface

Implemented by

Index

Methods

  • getHistoryMsgs(_options: { limit?: number; msgTypes?: string[]; reverse?: boolean; timetag?: number; done: any }): void
  • Retrieve the message history of a chat room..

    • The number of returned messages is less than or equal to `limit`. To facilitate the generation of timestamps in the conversation, several timestamp data are inserted in the returned result. Therefore, the total number of the returned message may be greater than limit
    • If reverse is set to false, return the time < timetag
    • If reverse is set to true, return the time > timetag
    • If reverse is set to false, return the messages in descending order by time. Messages received earlier are displayed at the end.
    • If reverse is set to true, return the messages in ascending order by time. Messages received later are displayed at the end
    • When querying by page, the results may contain duplicated message. It is recommended that you remove duplicates using idClient

    Parameters

    • _options: { limit?: number; msgTypes?: string[]; reverse?: boolean; timetag?: number; done: any }
      • Optional limit?: number

        limit. The default value 100

      • Optional msgTypes?: string[]

        Message types. All message types by default

      • Optional reverse?: boolean
        • false, return message time < timetag. The returned queue is sorted in descending order by time
        • true, return message time > timetag. The returned queue is sorted in ascending order by time
      • Optional timetag?: number

        timestamp in milliseconds. If unspecified, or the value is set to 0, its value will be affected by reverse

        • If reverse is set to false, the default value of timetag is the current server time
        • If reverse is set to true, the default value of timetag is 0
      • done:function

    Returns void

  • getHistoryMsgsByTags(options: { fromTime?: number; limit?: number; reverse?: 0 | 1; tags: string[]; toTime?: number; types: string[]; done: any }): void
  • Retrieve the message history of a chat room by tag.

    • The number of returned messages must be less than or equal to `limit`
    • If reverse is set to 0, query messages within time >= fromTime && time <= toTime
    • If reverse is set to 1, query messages within time >= fromTime && time <= toTime
    • If reverse is set to 0, return the messages in ascending order by time. Messages received later are displayed at the end
    • If reverse is set to 1, return the messages in descending order by time. Messages received earlier are displayed at the end.
    • When querying by page, the results may contain duplicated message. It is recommended that you remove duplicates using idClient

    Notes

    • If fromTime is specified, you must set toTime greater than fromTime, otherwise a parameter error will be reported
    • fromTime is set to 0 by default. The value of toTime is the current server time by default

    Parameters

    • options: { fromTime?: number; limit?: number; reverse?: 0 | 1; tags: string[]; toTime?: number; types: string[]; done: any }
      • Optional fromTime?: number

        Start time

      • Optional limit?: number

        Limit on quanity. The default value: 100.

      • Optional reverse?: 0 | 1

        The default value is 0.

        • A value of 0 indicates queries starts from fromTime backward. the returned messages are sorted in ascending order by time.
        • A value of 1 indicates queries start from toTime forward.. The returned messages are sorted in descending order by time
      • tags: string[]

        tags ['tag1', 'tag2', 'tag3']

      • Optional toTime?: number

        End time

      • types: string[]

        Message type

      • done:function

    Returns void

  • deprecated

    This interface is deprecated, use the sending interface instead.

    Resend a message using resendMsg after failing to send a message.

    Note: This interface is deprecated. To resend a message, use sendText, sendFile pass the parameter { resend: true }. For a code example, see {@link MessageInterface.sendFile | sendFile}

    Scope

    Calling this method triggers:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Parameters

    Returns NIMChatroomMessage

  • Description

    You can call this API for custom messages, such as rock-paper-scissors and dice rolling.

    Notes

    This interface returns the message body in the sending state, and the sent message body needs to be obtained by passing options.done.

    Scope

    Calling this API can trigger:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Example

    chatroom.sendCustomMsg({
    //The recipient receives the message using onMsg
    //if msg.type === 'custom', the recipient reads msg.content and then calls the business code
    content: JSON.stringify({type: 1}),
    done: function(err, msg) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', msg)
    }
    }
    })

    Parameters

    Returns NIMChatroomMessage

  • Description

    Send pictures, videos, audio or other files. You can optionally call sendFile to upload and send a file. You can also call previewFile to upload the file first, and then call sendFile to send the file

    Notes

    • This interface returns the message body in the sending state, and the sent message body needs to be obtained by passing options.done.

    Notes

    • Select one of the four parameters fileInput, file, blob, and filePath
    • fileInput: The id of the input DOM element with type='file'. Do not operate the file on this node until the upload is complete
    • file: Parameters of the previewFile function
    • blob: JavaScript object of Blob type
    • filePath: React Native, Mini Program and other special JS runtime environments (the temporary path obtained by chooseImage)

    Notes

    • type: image, audio, video or file. The default value is file. The information contained in the file object in the message body is different
    • image: url, name, size, ext, w, h, type
    • audio: url, name, size, ext, container, dur
    • video: url, name, size, ext, container, dur, w, h
    • file: url, name, size, ext

    Scope

    Calling this API can trigger:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Send a file

    chatroom.sendFile({
    type: 'image',
    fileInput: 'domId',
    done: function(err, msg) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', msg)
    }
    }
    })

    Upload a file using previewFile and send the file

    chatroom.previewFile({
    type: 'image',
    fileInput: fileInput,
    uploadprogress: function(obj) {
    console.log('Total size of a file: ' + obj.total + 'bytes');
    console.log('Size of uploaded data: ' + obj.loaded + 'bytes');
    console.log('Upload progress: ' + obj.percentage);
    console.log('Upload progress in percentage: ' + obj.percentageText);
    },
    done: function(error, file) {
    console.log('Uploading the image' + (!error?'success':'failure'));
    // show file to the user
    if (!error) {
    var msg = chatroom.sendFile({
    file: file,
    done: sendMsgDone
    });
    console.log('Sending the image message, id=' + msg.idClient);
    pushMsg(msg);
    }
    }
    })

    Resend a message

      let message = chatroom.sendFile({
    type: 'image',
    fileInput: 'domId',
    done: function(err, obj) {
    if (err) {
    console.log('Failed to send the message', err)
    // Resend the message. When the file upload fails, the msg of the obj parameter contains the message body, and in other cases, the obj is the message body.
    setTimeout(function () {
    resendMessage(obj.msg ? obj.msg : obj)
    }, 3000)
    } else {
    console.log('Message sent: ', obj)
    }
    }
    })

    // Get the idClient for rendering before uploading
    console.log(message.idClient)

    // Resend the message
    function resendMessage(oldMessage) {
    nim.sendFile(Object.assign(oldMessage, {
    type: 'image',
    fileInput: 'domId',
    resend: true, // Note that the resend is marked as true, so that the idClient in oldMessage can be used.
    done: function(err, obj) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', obj)
    }
    }
    }))
    }

    Associated links

    Parameters

    Returns NIMChatroomMessage

  • Description

    Send a location message

    Notes

    This interface returns the message body in the sending state, and the sent message body needs to be obtained by passing options.done.

    Scope

    Calling this API can trigger:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Example

    chatroom.sendGeo({
    //The recipient receives the message using onMsg
    //if msg.type === 'geo', the recipient reads msg.geo, and then calls the business code
    geo: {
    lng: 116.3833,
    lat: 39.9167,
    title: 'Beijing'
    },
    done: function(err, msg) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', msg)
    }
    }
    })

    Parameters

    Returns NIMChatroomMessage

  • Description

    Send a text message

    Notes

    This interface returns the message body in the sending state, and the sent message body needs to be obtained by passing options.done.

    Scope

    Calling this API can trigger:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Example

    chatroom.sendText({
    text: 'hello',
    done: function(err, msg) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', msg)
    }
    }
    })

    Parameters

    Returns NIMChatroomMessage

  • Description

    Sending an alert Alerts are sent for notifications in a conversation. Typical use cases include welcome messages when joining a group or tips when hitting keywords for moderation in chats.

    Notes

    This interface returns the message body in the sending state, and the sent message body needs to be obtained by passing options.done.

    Scope

    Calling this API can trigger:

    1. callback NIMChatroomGetInstanceOptions.onmsgs for recipients
    2. Callback NIMChatroomGetInstanceOptions.onmsgs triggered on other logged-in devices.

    Example

    chatroom.sendTipMsg({
    //The recipient receives the message using onMsg
    //if msg.type === 'tip', the recipient reads msg.tip, and then calls the business code
    tip: 'tip content',
    done: function(err, msg) {
    if (err) {
    console.log('Failed to send the message', err)
    } else {
    console.log('Message sent: ', msg)
    }
    }
    })

    Parameters

    Returns NIMChatroomMessage