How to Send and Receive Message Events

This guide covers how to send and receive Message Events with the Vonage Client SDK. Before you begin, make sure you added the SDK to your app, created a Session (Android, iOS, JS), and joined a Conversation.

Sending Message Text Events

Given a Conversation ID, you can send a Text Message Event.

client.sendMessageTextEvent(conversationId, "Hi Vonage!")
    .then(timestamp => {
        console.log("Successfully sent text message at ", timestamp);
    }).catch(error => {
        console.error("Error sending text message: ", error);
    });

Sending Message Image Events

Given a Conversation ID and URL for an image, you can send an Image Message Event.

client.sendMessageImageEvent(conversationId, imageURL)
    .then(timestamp => {
        console.log("Successfully sent image message at ", timestamp);
    }).catch(error => {
        console.error("Error sending image message: ", error);
    });

Sending Message Custom Events

Along with a Conversation ID, you can send a completely custom payload as a Custom Message Event. This is useful if you want a Message Event type that is specific to your application.

const attachmentPayload = {
    key1: "value 1",
    key2: "value 2"
};
      
client.sendMessageCustomEvent(conversationId, attachmentPayload)
    .then(timestamp => {
        console.log("Successfully sent custom message at ", timestamp);
    }).catch(error => {
        console.error("Error sending custom message: ", error);
    });

Receiving Message Events

You can receive all Conversation Message Events in your application by setting up an Event Listener/Delegate Function. Message Events received via this Listener/Delegate will automatically be updated to the delivered state. Here you can check the kind of incoming Message Event.

client.on("conversationEvent", event => {
    switch (event.kind) {
        case "message:text":
            handleTextMessage(event);
            break;
        case "message:image":
            handleImageMessage(event);
            break;
        case "message:custom":
            handleCustomMessage(event);
            break;
    };
});

Marking Message Events as Seen

Once you have received a Message Event, you can mark it as seen.

client.sendMessageSeenEvent(eventId, conversationId)
    .then(timestamp => {
        console.log("Successfully sent seen event at ", timestamp);
    }).catch(error => {
        console.error("Error sending seen event: ", error);
    });