Skip to content
Download all docs .md

Chat (JS)

Players can send messages to one player, to several players, or to a group. GameFuse stores the messages; your game shows them.

The player must be signed in. Chat is not live: a new message shows up when your game asks for it (see Check for new messages). For messages that arrive at once during a match, use realtime multiplayer.

Send a message

GameFuseChat.sendMessage starts a chat, or adds to the chat those players already have.

// To one player, by username
GameFuseChat.sendMessage('kim', 'rematch?', function (message, hasError) {
    if (hasError) {
        console.error('Could not send: ' + message);
        return;
    }
    // The chat is now first in the list
    const chat = GameFuseUser.CurrentUser.getDirectChats()[0];
    console.log(chat.getMessages()[0].getText());   // "rematch?"
});

// To several players: one chat with all of them
GameFuseChat.sendMessage(['kim', 'ava'], 'lobby at 8?', callback);

The first value can be a username, a list of usernames, a GameFuseUser, a list of them, or a GameFuseGroup.

Two shortcuts do the same thing:

friend.sendMessage('rematch?', callback);   // friend is a GameFuseUser
team.sendMessage('gg', callback);           // team is a GameFuseGroup: everyone in the group gets it

Load the player's chats

GameFuseChat.downloadChats(function (message, hasError) {
    if (hasError) {
        console.error('Could not load chats: ' + message);
        return;
    }
    const player = GameFuseUser.CurrentUser;
    for (const chat of player.getDirectChats()) {
        const names = chat.getParticipants().map(function (user) { return user.getUsername(); });
        const newest = chat.getMessages()[0];
        console.log(names.join(', ') + ': ' + (newest ? newest.getText() : ''));
    }
    console.log(player.getGroupChats().length + ' group chats');
});

downloadChats fills two lists on GameFuseUser.CurrentUser:

  • getDirectChats(): chats between players.
  • getGroupChats(): chats that belong to a group.

Each list has the newest chat first, and each chat has its newest 25 messages, newest first.

Show a chat and reply

const chat = GameFuseUser.CurrentUser.getDirectChats()[0];

// Oldest at the top, like a chat window
for (const message of chat.getMessages().slice().reverse()) {
    const who = message.getIsFromMe() ? 'me' : message.getUser().getUsername();
    console.log(who + ': ' + message.getText());
}

chat.sendMessage('ok, one more', function (message, hasError) {
    if (!hasError) {
        console.log(chat.getMessages()[0].getText());   // "ok, one more"
    }
});

A message:

Call Returns
message.getText() The text
message.getUser() Who sent it, a GameFuseUser
message.getIsFromMe() true if the signed-in player sent it
message.getCreatedAt() When it was sent
message.getIsRead() true if the signed-in player has read it
message.getReadBy() The IDs of the players who have read it
message.getID() The message's ID

Mark a message as read

const newest = chat.getMessages()[0];
if (!newest.getIsFromMe() && !newest.getIsRead()) {
    newest.markAsRead(function (message, hasError) { /* ... */ });
}

Older messages and chats

A chat comes with its newest 25 messages. Ask for the next 25 with a page number, starting at 2, in order.

chat.getOlderMessages(2, function (message, hasError) {
    if (!hasError) {
        console.log(chat.getMessages().length);   // up to 50 now, the older ones at the end
    }
});

// The same for the list of chats
GameFuseChat.getOlderChats(2, callback);

Check for new messages

Call GameFuseChat.downloadChats(callback) again: when the player opens your chat screen, and every few seconds while it is open. It replaces both lists with what is on GameFuse now.

Reference

Call What it does
GameFuseChat.sendMessage(recipients, text, callback) Sends a message, starting a chat if there is none
GameFuseChat.downloadChats(callback) Loads the player's chats and their newest messages
GameFuseChat.getOlderChats(page, callback) Loads older chats, from page 2
GameFuseUser.CurrentUser.getDirectChats(), getGroupChats() The loaded chats
chat.getID(), chat.getParticipants(), chat.getMessages() One chat
chat.sendMessage(text, callback) Replies in a chat
chat.getOlderMessages(page, callback) Loads older messages, from page 2
message.markAsRead(callback) Marks a message as read by the player
otherUser.sendMessage(text, callback), group.sendMessage(text, callback) Shortcuts for GameFuseChat.sendMessage