Send and Receive Text Messages

Overview

This guide covers sending and receiving messages within a conversation.

Before you begin, make sure you added the SDK to your app and you are able to create a conversation.

NOTE: A step-by-step tutorial to build a chat application is available here.

This guide will make use of the following concepts:

  • Conversation Events - message events that fire on a Conversation, after you are a Member

Send a Text Message

Given a conversation you are already a member of:

conversation.sendMessage({
    "message_type": "text",
    "text": "Hi Vonage!"
}).then((event) => {
    console.log("message was sent", event);
}).catch((error)=>{
    console.error("error sending the message ", error);
});
val messageText = "TEXT TO SEND"
val massage = NexmoMessage.fromText(messageText)

conversation.sendMessage(message, object : NexmoRequestListener<Void> {
    override fun onSuccess(p0: Void?) {
        Log.d("TAG", "Message has been sent")
    }

    override fun onError(apiError: NexmoApiError) {
        Log.d("TAG", "Error: Message not sent ${apiError.message}")
    }
})
String messageText = "TEXT TO SEND";
NexmoMessage massage = NexmoMessage.fromText(messageText);

conversation.sendMessage(message, new NexmoRequestListener<Void>() {
    public void onSuccess(@Nullable Void p0) {
        Log.d("TAG", "Message sent");
    }

    public void onError(@NotNull NexmoApiError apiError) {
        Log.d("TAG", "Error: Message not sent " + apiError.getMessage());
    }
});
let message = NXMMessage(text: "")
conversation.sendMessage(message, completionHandler: { [weak self] (error) in
   ...
})
NXMessage *message = [[NXMMessage alloc] initWithText:@""];
[conversation sendMessage:message completionHandler:^(NSError * _Nullable error) {
    ...
}];

Receive a Text Message

A message conversation event will be received when a member sends a text message to a conversation:

conversation.on('message', (sender, event) => {
    console.log('*** Message received', sender, event) 
});
private val messageListener = object : NexmoMessageEventListener {
    override fun onTypingEvent(typingEvent: NexmoTypingEvent) {}

    override fun onAttachmentEvent(attachmentEvent: NexmoAttachmentEvent) {}

    override fun onMessageEvent(messageEvent: NexmoMessageEvent) {
        val userName = messageEvent.embeddedInfo.user.name
        val text = messageEvent.message.text

        Log.d("TAG", "Message received. User $userName : $text")
    }

    override fun onSeenReceipt(seenEvent: NexmoSeenEvent) {}

    override fun onEventDeleted(deletedEvent: NexmoDeletedEvent) {}

    override fun onDeliveredReceipt(deliveredEvent: NexmoDeliveredEvent) {}
}

conversation?.addMessageEventListener(messageListener)
NexmoMessageEventListener messageListener = new NexmoMessageEventListener() {

    @Override
    public void onMessageEvent(@NonNull NexmoMessageEvent messageEvent) {
        String userName = messageEvent.getEmbeddedInfo().getUser().getName();
        String text = messageEvent.getMessage().getText();

        Log.d("TAG", "Message received. User " + userName + " : " + text);
    }

    @Override
    public void onAttachmentEvent(@NonNull NexmoAttachmentEvent attachmentEvent) {}

    @Override
    public void onEventDeleted(@NonNull NexmoDeletedEvent deletedEvent) {}

    @Override
    public void onSeenReceipt(@NonNull NexmoSeenEvent seenEvent) {}

    @Override
    public void onDeliveredReceipt(@NonNull NexmoDeliveredEvent deliveredEvent) {}

    @Override
    public void onTypingEvent(@NonNull NexmoTypingEvent typingEvent) {}
};

conversation.addMessageEventListener(messageListener);

Add NXMConversationDelegate as an extension to a ViewController or similar, and implement conversation(_ conversation: NXMConversation, didReceive event: NXMMessageEvent):

Note: The first method below is required when implementing NXMConversationDelegate:

extension ViewController: NXMConversationDelegate {
    func conversation(_ conversation: NXMConversation, didReceive error: Error) {
        NSLog("Conversation error: \(error.localizedDescription)")
    }
    func conversation(_ conversation: NXMConversation, didReceive event: NXMMessageEvent) {
        NSLog("Received message event: \(event.text ?? "")")
    }
}

Have a ViewController, or similar, conform to NXMConversationDelegate and implement conversation:didReceiveMessageEvent::

Note: The first method below is required when implementing NXMConversationDelegate:

- (void)conversation:(NXMConversation *)conversation didReceive:(NSError *)error {
    NSLog(@"Conversation error: %@", error.localizedDescription);
}
- (void)conversation:(NXMConversation *)conversation didReceiveMessageEvent:(NXMMessageEvent *)event {
    NSLog(@"Received message event: %@", event.text);
}

Reference