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:

NameTypeDescription
event_typeStringType of event to listen for
callbackFunctionFunction 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:

NameTypeDescription
usernameStringThe username of the user who sent the message (will always be the current user)
messageStringThe 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:

NameTypeDescription
usernameStringThe username of the user who sent the message
roomObjectAn object with name and id properties
messageStringThe message content
historyBooleanA 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: