The overview
This article explains how to use the LeanCloud IM SDK in the context of a game called “Humans Falling Out of Dreams”.
LeanCloud’s INSTANT messaging SDK can be used in a variety of scenarios, such as online customer service, live broadcast, group chat software, etc. This article introduces how to quickly implement friend chat in game scenarios. In game development, the need for instant chat is common, especially in multiplayer games, where instant chat is essential.
The LeanCloud IM SDK can be used for almost all in-game chat scenarios, such as open chat rooms in the game lobby, group room chat, instant chat with friends in the game.
In addition to sending basic text, picture, audio and video messages, it also supports custom message types, such as giving props, exchanging skins and other extended message types.
In group chat and open chat room and other business scenarios, we also provide sensitive content filtering, member role management (gag, kick, add, block, update the rights of others) and blacklist mechanisms to meet the diverse needs of product operation management.
LeanCloud provides SDKS for Android (Java), iOS (Objective-C/Swift), Unity, wechat Games and other platforms. Other game platforms, such as Cocos Creator and Egret engine, are also supported.
Start the game
First, let’s take a look at some of the instant messaging scenarios involved in the Human Falling Dream game. I’ll break them down into the following categories:
- World Channel (integrated chat room)
- Personal message
- Group chat (in-game chat)
Other games might
- Clan chat room (Clan, squad)
- Subscribe to channel messages
LeanCloud has four types of dialogue:
- Ordinary conversation
- The chat room
- System dialogue
- Temporary conversation
Here’s how to choose the type of conversation and what to do in each of these scenarios.
1. World Channel (integrated chat room)
As shown above, a “composite” chat, in which a large number of users interact in a group, has the following characteristics:
- Group chat is very large, the upper limit of group members is unlimited
- Online users can participate in the chat, no need to apply to join the group chat, join at any time, at any time to exit
- There is no fixed list of members, and members may be strangers without friends
- You do not need to receive push messages when offline
LeanCloud provides a type of chat room conversation for this scenario.
The chat room is perfect for this kind of game scene. It is customized and can be used immediately without much thought.
Small tips:
- Although the “chat room” in principle does not limit the number of members, but for the end user, that is, excessive messages constantly brush the screen, but affect the user experience. It is recommended to limit the number of people in each chat room to around 5000. Consider breaking up a large chat room into several smaller chat rooms at the application level.
- Since chat rooms don’t have a list of members, it makes no sense to specify members when you create them.
- Chat rooms have the concept of message priority, which can be considered when a connection is blocked and writes slowly due to too many messages. Low-priority messages are discarded when blocked, and high-priority messages are queued for delivery. By default, messages are of medium priority.
“Chat room” similar to the use of the scene, as well as live broadcast of the bullet screen. Like integrated chat rooms, bullet screens focus on the number of online members, not the specific list of members. If all participants enter the chat screen, they join the chat. If the chat screen is closed, they exit the chat. No member is invited to join the chat.
Two, friends of private messages
As shown in the picture above, in the “friends” chat, you can click the profile picture of any friend from the friend list to initiate a chat. The friends chat has the following features:
- There are only two people in the group chat, which is a private chat
- Chat records are saved persistently. Each time you initiate a chat, you need to load historical chat records
- Conversations between two friends are globally unique, and relaunching a chat will not create a new chat.
Friend conversations are one of the most common scenarios in instant messaging. In instant messaging as opposed to normal conversation — single/private conversation type.
Ordinary conversation and wechat chat function is similar, ordinary conversation is divided into single chat and group chat, in THE IM SDK single chat and group chat code level is no difference, but the number of single chat members (2), group chat members more than 2.
How ordinary conversation differs from “chat room” :
- A maximum of 500 members can be supported in a group chat
- You can view the member list, member online status, and member information
- You can view historical message records
- Members can be added or deleted
- When a member is offline, it can receive push notifications. When a member is online, it synchronizes messages
The IM SDK automatically enables the offline notification push service (provided that the push certificate required by each platform is correctly connected and the push configuration is correct). If offline push is not required, you can choose Application Settings > Security Center to disable Push Service.
Three, the team chat room
Other games also have a clan chat room scenario. This clan chat (or small group chat) has the same characteristics as your friends. The chat logs need to be stored in a persistent manner, and they correspond in the IM SDK to what they correspond in instant messaging to in regular conversations — the type of conversation in a group chat.
There is no difference between a group chat and a single chat except for the number of members.
4. Group Chat (in-game chat)
Send messages to teammates in the game
As shown in the picture, this group chat has the following characteristics:
- No need to save chat records, no need to query historical messages
- If the player suddenly goes offline, there is no need to send offline notifications
- The “room” is one-shot and disappears when the game is over
- Chat with a small number of participants (up to 8 in a group)
For this scenario, LeanCloud provides temporary dialogues, a type of dialogue that meets all of the above characteristics, right out of the box.
The message of the temporary conversation will not be persisted, that is, the historical conversation and chat records cannot be queried, the number of chat is up to 10.
Temporary conversations are similar to ordinary conversations in usage. The most important feature of temporary conversations is that they have a short validity period (they are not saved in the _Conversation table), and the advantage is that they can reduce the storage space occupied by persistent conversations stored on the server, thus reducing the cost of developers.
This kind of dialogue scene, common in some webpage popover customer service.
Small tip:
- with
createConversation
To create a normal conversation, usecreateChatRoom
Create a chat room withcreateTemporaryConversation
Create a temporary dialog.
5. Subscribe to channel news
In some games, users actively subscribe to channel messages, the type of messages that correspond to system conversations (also called service numbers) in the IM SDK. The process of service number is:
You can use the system dialog to send broadcast messages to all users. You can refer to the global broadcast document.
The system dialogue can be used to realize the robot automatic reply, public account, service account and other functions.
Began to chat
Step 1: Install the SDK
Reference: SDK installation guide for each platform
Step 2: Configure an independent API domain name
Reference: Domain Name Binding Guide
Step 3: Start with simple single chat, group chat, sending and receiving graphic messages
- One-on-one conversation
- Many people talk about
- Chat record query
Step 4: Explore more features
- More ways to send and receive messages, offline push and message synchronization, multiple device login
- Security and signature, blacklist and rights management, play with chat rooms and temporary conversations
- Elaborate message hook and system dialogue, build their own chatbot
Other Reference Documents
Instant messaging Service Overview LeanCloud Instant Messaging Usage Guide System dialogue usage overview of the messaging push service
summary
In addition to game scenarios, the IM SDK is also suitable for most IM scenarios, as long as you need to chat, you can choose LeanCloud SDK without any doubt, save effort and cost, and greatly shorten the development cycle.
Photo by Adam Solomon on Unsplash