. . .

Chat Module

Deprecated

Use the GroupChat Module instead!

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

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

images/download/attachments/27722199/db3-module-chat.PNG

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( boolean wasLoadedFromStorage, ApiomatRequestException exception )
{
if ( exception == null )
{
loadConversationModel( userName );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
 
AOMEmptyCallback loadMeCB = new AOMEmptyCallback( )
{
@Override
public void isDone( boolean wasLoadedFromStorage, ApiomatRequestException exception )
{
if ( exception == null )
{
loadConversationModel( userName );
}
else
{
user.saveAsync( saveCB );
}
}
};
user.loadMeAsync( loadMeCB );
}
Objective-C
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);
}];
}
}]; 
Swift
let userName = "JohnDoe"
let user = User(userName: userName, password: "123456")
DataStore.configureWithCredentials(user: user)
/* Login user */
user .loadMe { (error) in
if error == false {
 
loadConversationModel(userName);
} else {
 
/* Try to create new user */
user.save { (error) in
loadConversationModel(userName);
}
}
}
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);
}


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, boolean wasLoadedFromStorage, 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 );
}
Objective-C
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];
}
Swift
func loadConversationModel(userName: String) {
 
let subject = "Test subject"
let query = "subject==\(subject)")
let getConversationModelCB: CompletionHandlerWithResults = {(models, error) in
if error == false {
if models?.count == 0 {
createConversationModel(userName, subject)
} else if models?.count == 1 {
 
if let model = models[0] as? ConversationModel {
createMessage(model, userName )
}
} else {
print("Too many conversationModels.)
}
}
}
ConversationModel.loadList(query: query, usePersistentStorage: false, completion: 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);
}

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( boolean wasLoadedFromStorage, ApiomatRequestException exception )
{
if ( exception == null )
{
createMessage( conversationModel, userName );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
conversationModel.saveAsync( saveCB );
}
Objective-C
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 );
}
}];
}
Swift
func createConversationModel(userName: String, subject: String) {
 
var conversationModel = ConversationModel()
let userNames = [String]()
userNames.append(userName)
userNames.append("userName1")
userNames.append("userName2")
conversationModel.subject = subject
conversationModel.attendeeUserNames = userNames
conversationModel.save { (error) in
if error == false {
createMessage(conversationModel, userName);
}
}
}

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);
}

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( boolean wasLoadedFromStorage, ApiomatRequestException exception )
{
if ( exception == null )
{
loadMessages( conversationModel );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
AOMEmptyCallback saveCB = new AOMEmptyCallback( )
{
@Override
public void isDone( boolean wasLoadedFromStorage, ApiomatRequestException exception )
{
if ( exception == null )
{
message.sendAsync( conversationModel.getHref( ), messageSendCB );
}
else
{
Log.e( TAG, exception.getMessage( ) );
}
}
};
message.saveAsync( saveCB );
}
Objective-C
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];
}
Swift
func createMessage(conversationModel: ConversationModel, userName: String) {
 
var message = ChatMessageModel()
message.text = "Hi all"
message.senderUserName = userName
let messageSendCB: CompletionHandler = { (error) in
if error == false {
loadMessages(conversationModel)
}
}
let saveCB: CompletionHandler = { (error) in
if error == false {
message.send(conversationModel.href, completion: messageSendCB)
}
}
message.save(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);
}

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, boolean wasLoadedFromStorage, 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 );
}
Objective-C
void loadMessages(AOMConversationModel *conversationModel)
{
[conversationModel loadMessagesAsync:nil andWithResultBlock:^(NSMutableArray *models, NSError *error) {
if(error == FALSE)
{
for (AOMChatMessageModel *message in models) {
NSLog(@"%@", message.text);
}
}
}];
}
Swift
func loadMessages(conversationModel: ConversationModel) {
conversationModel.loadMessages { (models, error) in
if error == false {
if let messages = models as? [ChatMessageModel] {
for let message in messages) {
print(message.text)
}
}
}
}
}
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);
}

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?