. . .

Chat Module

Using the chat module you can create conversations with multiple other attendees. Attendees can be selected during a conversation from all your apps users.

Each user can send messages, including images or files and follow the other users messages in the conversation.

Configuration

Nothing to configure here.

Usage

The chat module provides you the class conversation model, a container for a conversation containing all attended users as a list and all messages, covered by the message model class.

At first create or load a user and configure the datastore:

Android
private void start( )
{
final User user = new User( );
final String userName = "John Doe";
user.setUserName( userName );
user.setPassword( "123456" );
Datastore.configureWithCredentials( user );
final AOMEmptyCallback saveCB = new AOMEmptyCallback( )
{
@Override
public void isDone( ApiomatRequestException exception )
{
if ( exception == null )
{
loadConversationModel( userName );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
 
AOMEmptyCallback loadMeCB = new AOMEmptyCallback( )
{
@Override
public void isDone( ApiomatRequestException exception )
{
if ( exception == null )
{
loadConversationModel( userName );
}
else
{
user.saveAsync( saveCB );
}
}
};
user.loadMeAsync( loadMeCB );
}
JavaScript
function start() {
//load a existing user and configure the datastore
var user=new Apiomat.User();
var userName="John Doe";
user.setUserName(userName);
user.setPassword("123456");
Apiomat.Datastore.configureWithCredentials(user);
/* save user */
var saveCB= {
onOk:function () {
loadConversationModel(userName);
},
onError:function(error) {
console.log(error);
}
};
 
/* load user */
var loadMeCB={
onOk:function () {
loadConversationModel(userName);
},
onError:function (error) {
if (error.statusCode==840) {
/* create a new Apiomat.User */
user.save(saveCB);
}
console.log(error);
}
};
user.loadMe(loadMeCB);
}
ObjectiveC
NSString *userName = @"JohnDoe";
AOMUser *user = [[AOMUser alloc] initWithUserName:userName andPassword:@"123456"];
[AOMDatastore configureWithCredentials:user];
/* Login user */
[user loadMeAsyncWithFinishingBlock:^(NSError *error) {
if(error == false)
{
loadConversationModel(userName);
}
else
 {
/* Try to create new user */
[user saveAsyncWithBlock:^(NSError *error) {
loadConversationModel(userName);
}];
}
}]; 


Now load an existing conversation model:


Android
private void loadConversationModel( final String userName )
{
final String subject = "Test subject";
AOMCallback<List<ConversationModel>> getConversationModelCB = new AOMCallback<List<ConversationModel>>( )
{
@Override
public void isDone( List<ConversationModel> objects, ApiomatRequestException exception )
{
if ( exception == null )
{
if ( objects.size( ) == 0 )
{
createConversationModel( userName, subject );
}
else if ( objects.size( ) == 1 )
{
createMessage( objects.get( 0 ), userName );
}
else
{
Log.e( TAG, "Too many conversationModels." );
}
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
ConversationModel.getConversationModelsAsync( "subject==\"" + subject + "\"", getConversationModelCB );
}
JavaScript
function loadConversationModel(userName) {
var subject="Test subject";
//first, get all conversationModels with subject
var getConversationModelCB={
onOk:function (objects) {
if (objects.length==0) {
/* no conversation models with subject found -> create a new conversation model */
createConversationModel(userName,subject);
} else if(objects.length==1) {
/* found conversation model --> create a new message */
createMessage(objects[0],userName);
} else {
console.log("Too many conversationModels.");
}
},
onError:function (error) {
console.log(error);
}
}
Apiomat.ConversationModel.getConversationModels ("subject==\""+subject+"\"", getConversationModelCB);
}
ObjectiveC
void loadConversationModel(NSString* userName)
{
NSString *subject = @"Test subject";
NSString *query = [NSString stringWithFormat:@"subject==\"%@\"", subject ];
AOMBlockWithResults getConversationModelCB = ^(NSMutableArray *models, NSError *error) {
if(error == FALSE)
{
if([models count] == 0)
{
createConversationModel( userName, subject );
}
else if ([models count] == 1)
{
createMessage( [models objectAtIndex:0], userName );
}
else
{
NSLog(@"Too many conversationModels.");
}
}
};
[AOMConversationModel getAsyncWithQuery:query withBlock:getConversationModelCB];
}

Or create a new conversation model:

Android
private void createConversationModel( final String userName, String subject )
{
final ConversationModel conversationModel = new ConversationModel( );
ArrayList<String> userNames = new ArrayList<String>( );
userNames.add( userName );
userNames.add( "userName1" );
userNames.add( "userName2" );
conversationModel.setSubject( subject );
conversationModel.setAttendeeUserNames( userNames );
AOMEmptyCallback saveCB = new AOMEmptyCallback( )
{
@Override
public void isDone( ApiomatRequestException exception )
{
if ( exception == null )
{
createMessage( conversationModel, userName );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
conversationModel.saveAsync( saveCB );
}
JavaScript
function createConversationModel(userName, subject) {
var conversationModel=new Apiomat.ConversationModel();
var userNames=[userName,"username1","username2"];
conversationModel.setSubject(subject)
conversationModel.setAttendeeUserNames(userNames);
var saveCB={
onOk:function () {
/* create a new message */
createMessage(conversationModel,userName);
},
onError: function (error) {
console.log(error);
}
};
conversationModel.save(saveCB);
}
ObjectiveC
void createConversationModel(NSString *userName, NSString *subject)
{
AOMConversationModel *conversationModel = [AOMConversationModel new];
NSMutableArray *userNames = [NSMutableArray new];
[userNames addObject:userName];
[userNames addObject:@"userName1"];
[userNames addObject:@"userName2"];
[conversationModel setSubject:subject];
[conversationModel setAttendeeUserNames:userNames];
[conversationModel saveAsyncWithBlock:^(NSError *error) {
if(error == FALSE)
{
createMessage( conversationModel, userName );
}
}];
}

After a conversation model exists, messages can be sent.

Android
private void createMessage( final ConversationModel conversationModel, String userName )
{
final ChatMessageModel message = new ChatMessageModel( );
long timestamp = new Date( ).getTime( );
message.setText( "Hi all " + timestamp );
message.setSenderUserName( userName );
final AOMEmptyCallback messageSendCB = new AOMEmptyCallback( )
{
@Override
public void isDone( ApiomatRequestException exception )
{
if ( exception == null )
{
loadMessages( conversationModel );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
AOMEmptyCallback saveCB = new AOMEmptyCallback( )
{
@Override
public void isDone( ApiomatRequestException exception )
{
if ( exception == null )
{
message.sendAsync( conversationModel.getHref( ), messageSendCB );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
message.saveAsync( saveCB );
}
JavaScript
function createMessage(conversationModel,userName) {
var message=new Apiomat.ChatMessageModel();
var timestamp=new Date().getTime();
message.setText("Hi all "+timestamp);
message.setSenderUserName(userName);
/* send message */
var messageSendCB = {
onOk:function () {
/* now get all messages */
loadMessages(conversationModel);
},
onError:function (error) {
console.log(error);
}
}
/* save message */
var saveCB={
onOk:function () {
message.send(conversationModel.getHref(),messageSendCB);
},
onError:function (error) {
console.log(error);
}
}
message.save(saveCB);
}
ObjectiveC
void createMessage(AOMConversationModel *conversationModel, NSString *userName)
{
AOMChatMessageModel *message = [AOMChatMessageModel new];
[message setText:@"Hi all"];
[message setSenderUserName:userName];
AOMEmptyBlock messageSendCB =^(NSError *error) {
if(error == FALSE)
{
loadMessages( conversationModel );
}
};
AOMEmptyBlock saveCB = ^(NSError *error) {
if(error == FALSE)
{
[message sendAsync:[conversationModel getHref] andWithBlock:messageSendCB];
}
};
[message saveAsyncWithBlock:saveCB];
}

Finally, all messages can be loaded.

Android
private void loadMessages( final ConversationModel conversationModel )
{
String query = "";
AOMCallback<List<ChatMessageModel>> loadMessagesCB = new AOMCallback<List<ChatMessageModel>>( )
{
@Override
public void isDone( List<ChatMessageModel> objects, ApiomatRequestException exception )
{
if ( exception == null )
{
for ( int i = 0; i < objects.size( ); i++ )
{
Log.i( TAG, objects.get( i ).getText( ) );
}
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
conversationModel.loadMessagesAsync( query, loadMessagesCB );
}
JavaScript
function loadMessages (conversationModel) {
var query="";
loadMessagesCB= {
onOk:function (objects) {
for (var i = 0; i < objects.length; i++) {
console.log(objects[i].getText());
}
},
onError:function (error) {
console.log(error);
}
};
conversationModel.loadMessages(query,loadMessagesCB);
}
ObjectiveC
void loadMessages(AOMConversationModel *conversationModel)
{
[conversationModel loadMessagesAsync:nil andWithResultBlock:^(NSMutableArray *models, NSError *error) {
if(error == FALSE)
{
for (AOMChatMessageModel *message in models) {
NSLog(@"%@", message.text);
}
}
}];
}
[message saveAsyncWithBlock:saveCB];
}

Showcase mobile app for the Android Play Store using ApiOmat Backend as a Service

After the chat module was included in our dashboard, we built an app called ChatOMat.

As you should be able to see the full code and learn how easy a interaction with ApiOmat is, the full code is available as open source at github. Just download and run!

Note: The ChatOMat uses my ApiOmat account by default. Hence, you won't be able to see the in coming data coming on the dashboard nor extend the functionality with own data models and such. At best, just

  • create your own app at the dashboard

  • don't forget to add the chat module,

  • deploy,

  • download the Android SDK,

  • and replace the content of the <em>frontend</em> directory with the classes from the zipped SDK

Let us know if you have any questions?