The overview

This article explains how to use the LeanCloud IM SDK in the context of the game “Human Falling Dreams” as an example.

LeanCloud instant messaging SDK can be used in a variety of scenarios, such as online customer service, live broadcast, work group chat software, etc. This article mainly introduces how to quickly implement friend chat function in game scenarios. The need for instant chat is common in game development, especially in multiplayer games.

LeanCloud IM SDK can be used for almost all in-game chat scenarios, such as the open chat room in the lobby of the game, in-room chat in groups, quick chat in the game and private messages to friends.

In addition to sending basic text, picture, audio and video messages, it also supports custom message types, such as gift props, exchange skin and other extended message types.

In group chat and open chat rooms and other business scenarios, we also provide mechanisms for filtering sensitive content, managing members’ roles (blocking, kicking, adding, blocking, updating others’ permissions) and blacklisting, so as to meet the diverse needs of product operation management.

For SDK selection, LeanCloud provides SDK for Android (Java), iOS (Objective-C/Swift versions), Unity, wechat mini games, etc. Other gaming platforms, such as Cocos Creator and Egret Engine, are also supported.

Start the game

First of all, let’s take a look at some of the im scenarios involved in the human Fall Dream game. I’ll break them down into the following:

  • World Channel (Integrated chat room)
  • Friend message
  • Team chat (in-game chat)

Other games might have it

  • Clan chat room (Clan, squad)
  • Subscribe channel messages

LeanCloud has designed four conversation types:

  • Ordinary conversation
  • The chat room
  • System dialogue
  • Temporary conversation

Here’s how to choose the type of dialogue and what to look for in each of these scenarios.

I. World Channel (comprehensive 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 number of people in the group is infinite
  • Online users can participate in the chat without applying to join the group chat. They can join at any time and quit at any time
  • There is no fixed list of members, and the members may be strangers without friends
  • There is no need to receive a notification push when offline

LeanCloud provides a chat room conversation type for this scenario.

The chat room is perfect for this kind of game scene, which is tailor-made, doesn’t require much thought, and can be used immediately.

Small tips:

  1. Although “chat room” does not limit the number of members in principle, for the end users, excessive messages constantly flood the screen, but affect the user experience. It is recommended to limit the number of people in each chat room to about 5000. Developers can consider splitting large chat rooms into smaller chat rooms at the application level.

  2. Chatrooms have no member lists, so it makes no sense to specify members when creating them.

  3. Chat rooms have the concept of message priority, which can be considered when a connection is blocked and slow to write due to too many messages. Low-priority messages are discarded when blocked, and high-priority messages are queued for delivery. By default, messages are medium priority.

“Chat room” similar use scenario, there are live bullets in the screen. Like comprehensive chat rooms, danmu only focuses on the number of online members, rather than a specific list of members. When all participants enter the chat interface, they join, and when they close the interface, they quit. No member is invited to join.

Two, friends message

As shown in the picture above, in “friends” chat, you can click any friend’s profile picture from the friend list to initiate a chat. Friend chat has the following features:

  • There are only two people in the group, so it’s a private chat
  • Chat records are stored persistently. You need to load historical chat records each time you initiate a chat
  • Conversations between two friends are globally unique, and initiating another chat will not create a new chat.

Friend conversations are one of the most common scenarios in instant messaging. In im versus normal conversation — single chat/private chat type.

The functions of ordinary conversations are similar to those of wechat chat. Ordinary conversations are divided into single and group chats. In IM SDK, the code level of single and group chats is the same, but the number of members of a single chat is 2, while the number of group chats is more than 2.

The difference between a normal conversation and a chat room:

  1. There is a maximum of 500 members in a group chat
  2. You can view member list and online status, and pay attention to member information
  3. Historical message records are available
  4. Allow adding and deleting members
  5. When members are offline, they can receive notification notifications and synchronize messages after they go online

The IM SDK automatically enables the offline message 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 turn off the push service switch by choosing Application Settings > Security Center.

Team chat room

Other games have the scene of a clan chat room, where in-clan chats (or small group chats) have the same characteristics as friends, and chat logs need to be stored persistently, which in the IM SDK corresponds to instant messaging and regular conversations — the type of conversation that a group chat has.

There is no difference between group chat and single chat except the number of members.

Group chat (in-game chat)

Send messages to teammates in the game

As shown in the figure, this group chat has the following characteristics:

  • There is no need to save chat records and no need to query historical messages
  • If the player is suddenly offline, there is no need to send an offline notification
  • The “room” is disposable and disappears when the game ends
  • Small number of participants in chat (maximum 8 people in groups)

For this scenario, LeanCloud provides a temporary conversation type that meets all of the above features and is also out of the box.

Temporary chat information will not be persisted, that is, historical chat and chat records cannot be queried. The number of chat users is 10.

Temporary conversations are similar to regular conversations in usage, but have a short lifetime (they are not stored in the _Conversation table). The advantage is that they reduce the storage space occupied by the server for the persistent storage of the conversation, thus reducing the cost of using the conversation.

This kind of dialogue scene, common in some web page popup customer service.

Small tip:

  • withcreateConversationTo create a normal dialog, usecreateChatRoomCreate a chat room withcreateTemporaryConversationCreate temporary conversations.

Fifth, subscribe channel news

In some games, there are scenarios where users actively subscribe to channel messages, which correspond to system conversations (also called service numbers) in the IM SDK. The process of service number is as follows:

Use the system dialog to send broadcast messages to all users, you can refer to the global broadcast documentation.

System dialogue can be used to realize 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 the independent API domain name

Reference: Domain name binding Guide

Step 3: Start from simple single chat, group chat, send and receive text messages

  1. One to one chat
  2. Many people talk about
  3. Chat History Query

Step 4: Explore more features

  1. More ways to send and receive messages, offline push and message synchronization, multi-device login
  2. Security and signature, blacklist and permission management, chat rooms and temporary conversations
  3. Explain the message hook and the system dialogue, build their own chat robot

Other Reference Documents

Overview of instant messaging services

LeanCloud Instant Messaging Guide

Use of system dialogs

Overview of message push services

summary

In addition to game scenarios, the IM SDK is also suitable for most instant messaging scenarios. If you need to chat, you can choose the LeanCloud SDK without any doubt, save effort, save cost, and greatly shorten the development cycle.

Photo by Adam Solomon on Unsplash