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 simple conversation.

NOTE: A step-by-step tutorial to building a web-based chat application is also available for JavaScript and iOS.

This guide will make use of the following concepts:

  • Conversation Events - text 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.sendText(message).then(() => {
    ...
}).catch(errorLogger)
conversation.sendText(message, completionHandler: { [weak self] (error) in
   ...
})
[conversation sendText:message completionHandler:^(NSError * _Nullable error) {
    ...
}];

Receive a Text Message

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

conversation.on('text', (sender, event) => {
    console.log('*** Message received', sender, event) 
});

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

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: NXMTextEvent) {
        NSLog("Received text event: \(event.text ?? "")")
    }
}

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

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 didReceiveTextEvent:(NXMTextEvent *)event {
    NSLog(@"Received text event: %@", event.text);
}