SightCall Developer Center

Account Login Support

Chat Feature

Overview

This tutorial shows you how to exchange data between contacts, and acknowledge reception of this data.

A code sample implementing the chat feature is available on GitHub.

Prerequisites

To use the messaging feature, the user needs to be registered to the presence server. This is done automatically during connection. Messaging is available once the event presence.ok has been received.

Send a message to someone

Sending a message to a contact is as simple as calling the Rtcc.sendMessage method. Three parameters are in the prototype of the method:

var message = "on the cloud";
msg_id = 0;

rtcc.sendMessage(msg_id, uidOfRecipient, message);
NSString *message = @"Alright!";
NSArray *recipient = @[@"recipient"];
[[Rtcc instance] dataSend:[message dataUsingEncoding:NSUTF8StringEncoding] toContactUIDs:recipient withID:0];
let message: String = "Alright!"
let recipient: [String] = ["recipient"]
Rtcc.instance().dataSend(message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) toContactUIDs:recipient, withID:0 )
Rtcc.instance().dataChannel().send("Greetings!".getBytes(), id, "JellyBean", "KitKat", "Lollipop");

msg_id can be constant or can be different from one message to another.In order to be able to acknowledge a message at reception, it's important to have a unique id for each message.

Receive a Message

Received messages trigger the event message.receive.


function onMessageReceived (msg_id, uidOfSender, message){
    console.log('message received from ' + uidOfSender + ' :' + message);
}

rtcc.on('message.receive',onMessageReceived);
//To receive message the delegate shall be set
[[Rtcc instance] setDataDelegate:theDataDelegate];

// in RtccDataDelegate's
- (void)rtccReceivedData:(NSData *)data from:(NSString *)contactUID withID:(uint8_t)messageID {
    ...
}
//To receive message the delegate shall be set
Rtcc.instance().dataDelegate = theDataDelegate

// in RtccDataDelegate's
func rtccReceivedData(data: NSData!, from contactUID: String!, withID messageID: UInt8) {
    ...
}
@RtccEventListener
public void onDataChannelOutOfBandEvent(DataChannelOutOfBandEvent event) {
    String from = event.getUid();
    String message = new String(event.getPayload());
}

Acknowledge a Message

To acknowledge reception of the message, use acknowledgeMessage.

Three parameters are required:

An auto-acknowledge sample could be:


function onMessageReceived (msg_id, uidEmitter, message){
    console.log('message received from '+uidEmitter+' :' + message);
    rtcc.acknowledgeMessage(msg_id,uidEmitter,1);
}

rtcc.on('message.receive',onMessageReceived);
//in RtccDataDelegate
- (void)rtccReceivedData:(NSData *)data from:(NSString *)contactUID withID:(uint8_t)messageID {
    ...
    [[Rtcc instance] dataAcknowledge:messageID ofContact:contactUID withFlag:2];
    ...
}
//in RtccDataDelegate
func rtccReceivedData(data: NSData!, from contactUID: String!, withID messageID: UInt8) {
    ...
    Rtcc.instance().dataAcknowledge(messageID, ofContact: contactUID, withFlag: 0)
    ...
}
public void acknowledge(String uid, int messageId, int value) {
    Rtcc.instance().dataChannel().ack(uid, messageId, value);
}

// or simpler

@RtccEventListener
public void onDataChannelOutOfBandEvent(DataChannelOutOfBandEvent event) {
    event.ack(1);
}

The sender of the original message is then notified:


function onAcknowledgeReceived (message_id, uid, status){
    console.log('message acknowledged from '+uid+' :' +'id: '+message_id + 'ack: '+status);
}

rtcc.on('message.acknowledge',onAcknowledgeReceived);
//in RtccDataDelegate
- (void)rtccReceivedAckForID:(uint8_t)messageID fromContact:(NSString *)contactUID withFlag:(uint8_t)flag{
    ...
    NSLog(@"Message %i acknowledge from %@ with %i",messageID,contactUID,flag);
    ...
}
func rtccReceivedAckForID(messageID: UInt8, fromContact contactUID: String!, withFlag flag: UInt8) {
    ...
    println("Message \(messageID) acknowledge from \(contactUID!) with \(flag)");
    ...
}
@RtccEventListener
public void onDataChannelAckEvent(DataChannelAckEvent event) {
    Log.d(TAG, event);
}