Listen for chat messages from the user
addEventListener(event_type, callback)

Listening for chat text entry

The chat property on the Kongregate API object broadcasts several different types of events. The message event lets you know when the player has submitted a message in the custom chat tab, while the room.message event notifies you of messages from other users in game chat, allowing you to listen in and react.

The addEventListener method takes two arguments, the event type, and a callback function:

Name

Type

Description

event_type

String

Type of event to listen for

callback

Function

Function to call when the event is triggered

The message event

The message event is broadcast when the player submits text in the entry box on your custom tab. It has a single data field with the following properties:

Name

Type

Description

username

String

The username of the user who sent the message (will always be the current user)

message

String

The message content

Example: Listen for the player submitting a chat message in a custom tab

function onPlayerMessage(event:*):void {
  trace("Message from " + event.data.username + ": " + event.data.message);
}

kongregate.chat.addEventListener("message", onPlayerMessage);
function onPlayerMessage(event) {
  console.log("Message from " + event.data.username + ": " + event.data.message);
}

kongregate.chat.addEventListener("message", onPlayerMessage);

The room.message event

The room.message event is broadcast when a game chat message is received. It has a single data field with the following properties:

Name

Type

Description

username

String

The username of the user who sent the message

room

Object

An object with name and id properties

message

String

The message content

history

Boolean

A flag indicating whether or not the message was retrieved from the room history or not

Example: Listen for game chat messages

function onRoomMessage(event:*):void {
  trace("GameChat: " + event.data.username + ": " + event.data.message);
}

kongregate.chat.addEventListener("room.message", onRoomMessage);
function onRoomMessage(event) {
  trace("GameChat: " + event.data.username + ": " + event.data.message);
}

kongregate.chat.addEventListener("room.message", onRoomMessage);

The tab_visible event

The tab_visible event is broadcast when a custom tab has been shown after a call to showTab

function onTabVisible(event:*):void {
  trace("Tab visible!");
}

kongregate.chat.addEventListener("tab_visible", onTabVisible);
function onTabVisible() {
  console.log("Tab visible!");
}

kongregate.chat.addEventListener("tab_visible", onTabVisible);

Related: