AppWarp is known best for real time communication and this feature has been utilized by game developers to build highly engaging multiplayer games and by app developers to integrate real time messaging in their apps. The chat feature helped in boosting user engagement in many strategy, card, casino, racing, shoot out, etc games along with enabling users to exchange messages in real time in e-commerce, banking, media, etc apps.
In the current scenario once disconnected, the users could not see the messages they had previously exchanged even after recovering their last session. To sort out this issue we have launched the latest feature called AppWarp Chat History, using which you can always check out previously exchanged messages in any game or app powered by AppWarp. The benefits of enabling AppWarp Chat History are:
- Even if your users’ connection breaks, they can quickly log back in and can continue from where they left off
- In any multiplayer card game, if a new player joins, they can check out the previously exchanged messages to get the hang of what exactly is happening in the room
- If a user logs out accidently, they can log back in any time in that particular room without losing out the previous chat
Getting Started: Configuring AppWarp Chat History is very simple and following steps will help you in setting up your app on AppWarp platform for chat history:
– Login or Register with App42 Platform
– Create an application if you have not created yet
– Create a Database in Storage section to store chat messages
– Download AppWarp SDK – Add the SDK in your project
Let’s dive-in: If you are new to AppWarp, I recommend you to go through our Development Center to choose the platform you are working on. Our detailed documentation helps you in understanding how to get started or how to use a particular API.
When you are done with the SDK integration, following steps will help you in setting up your project to use AppWarp as well as its new chat history feature:
1. SDK Initialization : [code java]WarpClient.initialize(Constants.apiKey, Constants.secretKey);
//Create Object for WarpClient
theClient = WarpClient.getInstance();[/code] 2. Add Callback Listeners : [code java]theClient.addConnectionRequestListener(this);
theClient.addZoneRequestListener(this);
theClient.addRoomRequestListener(this);
theClient.addNotificationListener(this);
// Get Chat History Callback
theClient.addChatRequestListener(this);[/code] 3. Enable Chat History and Set DB Name : You have already created a database in Storage section of AppHQ, so let’s use it. Put the following code snippet with your database name just after initialization:
[code java]theClient.setDbName(“DB Name”);
theClient.enableChatHistory(true);[/code] 4. Sending Chat Messages : To start messaging among peers, the user will need to connect to the AppWarp server, join and then subscribe a room. Once that user is connected and joined that room, you can use the following code snippet to start chatting:
[code java]theClient.sendChat(message, saveHistory, roomId);
[/code]
5. Getting the History : When you need to retrieve chat history for a specific room based on your game business logic, use the following code snippet:
[code java]theClient.getChatHistory(roomId, max, offset);
[/code]
Let’s discuss the parameters passed in the above code one-by-one: roomID: RoomID for which history needs to be fetched max: The number of messages that need to be fetched. You can fetch a maximum of 100 messages in one go. offset: The index from where the chat messages needs to be fetched. For example, If there are 100 messages and you already have first 10 messages with you, now if you need to fetch next 20 messages then offset will 10 and max will be 20.
6. Handling the Chat History CallBack : The history will be provided to you in onGetChatHistoryDone callback method of ChatRequestListener which you can handle as follows:
[code java]@Override
public void onGetChatHistoryDone(byte result,ArrayList chatHistory) {
if(result==WarpResponseResultCode.SUCCESS){
for(ChatEvent chatEvent:chatHistory){
System.out.println(“Sender :” +chatEvent.getSender());
System.out.println(“Meesage :” +chatEvent.getMessage());
System.out.println(“Time Stamp :” +chatEvent.getTime());
}
}
else{
System.out.println(“Error Code :” +result);
}
}[/code]
7. Chat History Life : The life of the chat history is directly associated with the life of the room. The history of a room of type Dynamic will automatically be deleted when the room is deleted from the server. In case of Static room, the history will not be deleted by the server because the static room does not get deleted automatically.
You can also view the chat history as admin of the app from the AppHQ management console as follows:
- Go to AppHQ dashboard
- Select Storage section
- Select JSON feature
- In Chat History collection you will see different messages
Congratulations! you have configured your app in AppHQ dashboard as well as your client side project for AppWarp chat history feature. For more info about ApppWarp API, you can visit AppWarp Dev Center or write to us at support@shephertz.com, our customer success team is always excited to hear from you.
Leave A Reply