Skip to main content

Create General Player Chat


This tutorial will show you how to create a general player chat, send and receive messages from the chat, and subscribe to events. The sequence of actions in the chat, which we will consider:

  • create a chat in the game control panel
  • Player 1 joining the chat
  • getting a list of messages from a chat
  • send a message to the chat
  • subscribe to notification about sending messages in real time
  • Player 2 joining the chat and sending messages
  • checking the online players in the chat

Let's consider the actions at each step in more detail.

Create a Chat

You can create a general chat using the channel functions. That is, we will create a channel that will play the role of a general chat. To do this, go to the control panel games, select the “Channels - Add channel” section:

In the channel name field, enter an arbitrary name, for example, Chat. Leave the rest of the channel settings as default, click the “Save channel” button. This will create a channel - chat. In our example, it has ID: 90:

Enter Player into Chat

When the Player enters the game, he can join the created channel by ID:{channelID:90})

You can check the presence of a player in the channel from the game control panel. To do this, click on the name of the created channel Chat and press the button for selecting participants. A list of players in the channel will be shown. In our example, one player with ID: 1807696 joined the chat:

Actions in the Channel

After joining the chat, Player 1 can receive messages. To do this, use the fetchMessages method, in which you need to pass the channel ID:


If everything is done correctly, then the promise will contain information that there are no messages in the chat yet. We will repeat the request later, when we add the second participant to the chat - Player 2.

In the meantime, we send a message on behalf of Player 1:

gp.channels.sendMessages({channelId:90, text: `privet`})

At the same time, all chat participants receive an event:message event with information that “someone” sent a message to the channel. In addition to event:message, the sender also receives a response from the server, which contains:

  • Id of the player - the sender of the message
  • text - text
  • dispatch date - createdAt
  • information about the sender player. At the same time, the player player has all public fields listed: name, avatar, etc. That is, if you wish, you can draw the sender's avatar and show all public information in the game interface:

All other chat participants must subscribe to this notification. And as soon as the message arrives, you need to display it somewhere. As an example, let's subscribe to an event about what "someone" wrote in the chat. We subscribe to an event and say which function to call if this event occurs:

gp.channels.on('event:message', (message) => {
//display a message in the chat (add a new message)
console.log(`игрок #${}: ${message.text}`);

That is, we subscribed to the event and are waiting for other players to write to the general chat.

Sending messages to the second player

As an experiment, we will send a message to the same chat by another player. But first let's add Player 2:{channelID:90})

We request a list of messages in the chat on behalf of Player 2:


In the promise, we see information that there is one message in the chat. The same privet message sent by Player 1:

Sending a message to the channel on behalf of Player 2:

gp.channels.sendMessages({channelId:90, text: `Hello`})

At the same time, an event came to the chat that the player sent a Hello message:

It is also worth noting that from the control panel you can monitor transmitted messages and write to the chat on behalf of the game administrator:

If the administrator writes a message from the control panel, then all players in the chat will receive messages from the player with Id: 0 (a zero player ID allows him to be identified as an administrator):

You can also see the list of players in the chat using fetchMembers:

gp.channels.fetchMembers({channelId: 90})

In our example, the promise will contain two participants (Player 1 and Player 2), as well as general information about the players (mute or not, avatar, amount of gold, etc.). Separately, the isOnline status is shown, which allows you to check which of the chat participants is online:

Check Online Players

At the same time, there is an additional property on query that allows you to query users who are online. To do this, you can pass isOnline:true as an additional argument to the fetchMembers method. In this case, only those players who are online will be returned:

If someone leaves the chat, all chat participants will receive a notification:

So, a channel created with default settings can be used as a general chat. All other players can join the chat, receive / send messages and subscribe to notifications. We hope that this functionality will be useful to you.

Stay in Touch

Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.

GamePush Community Telegram: @gs_community.

For your suggestions e-mail: [email protected]

We Wish you Success!