{"_id":"571011ae10300c0e007f614a","category":{"_id":"570a5676ade45d0e00c1ad33","__v":0,"project":"56abbf55f25f160d00e17f4e","version":"56abbf55f25f160d00e17f51","sync":{"url":"","isSync":false},"reference":true,"createdAt":"2016-04-10T13:34:46.864Z","from_sync":false,"order":3,"slug":"client-side-api","title":"Client Side API"},"parentDoc":null,"version":{"_id":"56abbf55f25f160d00e17f51","project":"56abbf55f25f160d00e17f4e","__v":12,"createdAt":"2016-01-29T19:36:53.665Z","releaseDate":"2016-01-29T19:36:53.665Z","categories":["56abbf56f25f160d00e17f52","56abca6bf9757e0d007c6650","56acddfa0ab3c00d00ce3332","56af65da9d32e30d0006d30f","56af66cab34d210d003d9ad0","56af6afcd21e9c0d00b628d1","56af6d6ecc4cbd0d00ce2c88","5705b12221cfed0e00e8c580","570a5676ade45d0e00c1ad33","570d7d25d1e4b82000d9e385","570eac3c3160d10e0041df0e","575709000fd6a3200010dded"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"56abbf55f25f160d00e17f4e","user":"56abbec30b9e0b0d00616274","__v":6,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-04-14T21:54:54.599Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":true,"order":0,"body":"### Listening for chat text entry\n\nThe `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.\n\nThe `addEventListener` method takes two arguments, the event type, and a callback function:\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Type\",\n    \"h-2\": \"Description\",\n    \"0-0\": \"`event_type`\",\n    \"0-1\": \"String\",\n    \"0-2\": \"Type of event to listen for\",\n    \"1-0\": \"`callback`\",\n    \"1-1\": \"Function\",\n    \"1-2\": \"Function to call when the event is triggered\"\n  },\n  \"cols\": 3,\n  \"rows\": 2\n}\n[/block]\n### The `message` event\n\nThe `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:\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Type\",\n    \"h-2\": \"Description\",\n    \"0-0\": \"`username`\",\n    \"0-1\": \"String\",\n    \"0-2\": \"The username of the user who sent the message (will always be the current user)\",\n    \"1-0\": \"`message`\",\n    \"1-1\": \"String\",\n    \"1-2\": \"The message content\"\n  },\n  \"cols\": 3,\n  \"rows\": 2\n}\n[/block]\n**Example:** Listen for the player submitting a chat message in a [custom tab](doc:chatshowtab)\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function onPlayerMessage(event:*):void {\\n  trace(\\\"Message from \\\" + event.data.username + \\\": \\\" + event.data.message);\\n}\\n\\nkongregate.chat.addEventListener(\\\"message\\\", onPlayerMessage);\",\n      \"language\": \"javascript\",\n      \"name\": \"ActionScript 3\"\n    },\n    {\n      \"code\": \"function onPlayerMessage(event) {\\n  console.log(\\\"Message from \\\" + event.data.username + \\\": \\\" + event.data.message);\\n}\\n\\nkongregate.chat.addEventListener(\\\"message\\\", onPlayerMessage);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n### The `room.message` event\n\nThe `room.message` event is broadcast when a game chat message is received. It has a single `data` field with the following properties:\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Name\",\n    \"h-1\": \"Type\",\n    \"h-2\": \"Description\",\n    \"0-0\": \"`username`\",\n    \"0-1\": \"String\",\n    \"0-2\": \"The username of the user who sent the message\",\n    \"1-0\": \"`room`\",\n    \"1-1\": \"Object\",\n    \"1-2\": \"An object with `name` and `id` properties\",\n    \"2-0\": \"`message`\",\n    \"2-1\": \"String\",\n    \"2-2\": \"The message content\",\n    \"3-0\": \"`history`\",\n    \"3-1\": \"Boolean\",\n    \"3-2\": \"A flag indicating whether or not the message was retrieved from the room history or not\"\n  },\n  \"cols\": 3,\n  \"rows\": 4\n}\n[/block]\n**Example:** Listen for game chat messages\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function onRoomMessage(event:*):void {\\n  trace(\\\"GameChat: \\\" + event.data.username + \\\": \\\" + event.data.message);\\n}\\n\\nkongregate.chat.addEventListener(\\\"room.message\\\", onRoomMessage);\",\n      \"language\": \"javascript\",\n      \"name\": \"ActionScript 3\"\n    },\n    {\n      \"code\": \"function onRoomMessage(event) {\\n  trace(\\\"GameChat: \\\" + event.data.username + \\\": \\\" + event.data.message);\\n}\\n\\nkongregate.chat.addEventListener(\\\"room.message\\\", onRoomMessage);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## The `tab_visible` event\n\nThe `tab_visible` event is broadcast when a custom tab has been shown after a call to `showTab`\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function onTabVisible(event:*):void {\\n  trace(\\\"Tab visible!\\\");\\n}\\n\\nkongregate.chat.addEventListener(\\\"tab_visible\\\", onTabVisible);\",\n      \"language\": \"javascript\",\n      \"name\": \"ActionScript 3\"\n    },\n    {\n      \"code\": \"function onTabVisible() {\\n  console.log(\\\"Tab visible!\\\");\\n}\\n\\nkongregate.chat.addEventListener(\\\"tab_visible\\\", onTabVisible);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nRelated:\n\n* [Custom Chat](doc:api-custom-chat) \n* [Chat.ClearMessages](doc:client-api-chat-clearmessages) \n* [Chat.CloseTab](doc:client-api-chat-closetab) \n* [Chat.DisplayMessage](doc:client-api-chat-displaymessage) \n* [Chat.ShowTab](doc:client-api-chat-showtab)","excerpt":"Listen for chat messages from the user\n`addEventListener(event_type, callback)`","slug":"client-api-chat-addeventlistener","type":"fn","title":"Chat.AddEventListener"}

Chat.AddEventListener

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: [block:parameters] { "data": { "h-0": "Name", "h-1": "Type", "h-2": "Description", "0-0": "`event_type`", "0-1": "String", "0-2": "Type of event to listen for", "1-0": "`callback`", "1-1": "Function", "1-2": "Function to call when the event is triggered" }, "cols": 3, "rows": 2 } [/block] ### 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: [block:parameters] { "data": { "h-0": "Name", "h-1": "Type", "h-2": "Description", "0-0": "`username`", "0-1": "String", "0-2": "The username of the user who sent the message (will always be the current user)", "1-0": "`message`", "1-1": "String", "1-2": "The message content" }, "cols": 3, "rows": 2 } [/block] **Example:** Listen for the player submitting a chat message in a [custom tab](doc:chatshowtab) [block:code] { "codes": [ { "code": "function onPlayerMessage(event:*):void {\n trace(\"Message from \" + event.data.username + \": \" + event.data.message);\n}\n\nkongregate.chat.addEventListener(\"message\", onPlayerMessage);", "language": "javascript", "name": "ActionScript 3" }, { "code": "function onPlayerMessage(event) {\n console.log(\"Message from \" + event.data.username + \": \" + event.data.message);\n}\n\nkongregate.chat.addEventListener(\"message\", onPlayerMessage);", "language": "javascript" } ] } [/block] ### 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: [block:parameters] { "data": { "h-0": "Name", "h-1": "Type", "h-2": "Description", "0-0": "`username`", "0-1": "String", "0-2": "The username of the user who sent the message", "1-0": "`room`", "1-1": "Object", "1-2": "An object with `name` and `id` properties", "2-0": "`message`", "2-1": "String", "2-2": "The message content", "3-0": "`history`", "3-1": "Boolean", "3-2": "A flag indicating whether or not the message was retrieved from the room history or not" }, "cols": 3, "rows": 4 } [/block] **Example:** Listen for game chat messages [block:code] { "codes": [ { "code": "function onRoomMessage(event:*):void {\n trace(\"GameChat: \" + event.data.username + \": \" + event.data.message);\n}\n\nkongregate.chat.addEventListener(\"room.message\", onRoomMessage);", "language": "javascript", "name": "ActionScript 3" }, { "code": "function onRoomMessage(event) {\n trace(\"GameChat: \" + event.data.username + \": \" + event.data.message);\n}\n\nkongregate.chat.addEventListener(\"room.message\", onRoomMessage);", "language": "javascript" } ] } [/block] ## The `tab_visible` event The `tab_visible` event is broadcast when a custom tab has been shown after a call to `showTab` [block:code] { "codes": [ { "code": "function onTabVisible(event:*):void {\n trace(\"Tab visible!\");\n}\n\nkongregate.chat.addEventListener(\"tab_visible\", onTabVisible);", "language": "javascript", "name": "ActionScript 3" }, { "code": "function onTabVisible() {\n console.log(\"Tab visible!\");\n}\n\nkongregate.chat.addEventListener(\"tab_visible\", onTabVisible);", "language": "javascript" } ] } [/block] Related: * [Custom Chat](doc:api-custom-chat) * [Chat.ClearMessages](doc:client-api-chat-clearmessages) * [Chat.CloseTab](doc:client-api-chat-closetab) * [Chat.DisplayMessage](doc:client-api-chat-displaymessage) * [Chat.ShowTab](doc:client-api-chat-showtab)