This page documents the primary methods and usage of the chattable
object used to interface with an embedded Chattable iframe. To use the Library/API, you must have a Chattable iframe embedded on your site. It is recommended that you hotlink this script from https://iframe.chat/scripts/main.min.js instead of downloading it to ensure Chattable is using the latest code and remains functional on your site. [Terms of Service]
chattable.initialize(parameters)
Initializes the chattable interface. Waits for the iframe to be present, and then applies either a custom stylesheet or a named demo (theme). Your chat waits for this to run in order to load. Your iframe must have the ID chattable
for this to run.
Name | Type | Description | Optional |
---|---|---|---|
stylesheet |
String | URL of a CSS file to load (relative or absolute) | Yes |
theme |
String | Name of a predefined theme | Yes |
none |
- | If no parameters are given, the chat will load with no styles applied | Yes |
chattable.initialize({ stylesheet: "styles/chattable.css" });
chattable.initialize().then(callback)
Runs a callback function after the iframe initializes.
chattable.initialize().then(function(){ let name = chattable.user.name; });
chattable.reinitialize(parameters)
Reloads styles and resends command registry to the iframe. Use this if the iframe reloads unexpectedly or needs to re-sync.
Name | Type | Description | Optional |
---|---|---|---|
stylesheet |
String | URL of a CSS file to load (relative or absolute) | Yes |
theme |
String | Name of a predefined theme | Yes |
none |
- | If no parameters are given, the chat will load with no styles applied | Yes |
chattable.sendMessage(text, name?, flair?, perms?)
Sends a chat message to the iframe.
Name | Type | Description | Optional |
---|---|---|---|
text |
String | Message body | No |
name |
String | Sender name override | Yes |
flair |
String | Flair for the message | Yes |
perms |
Boolean | Send as current user (true) or a bot (false). Default: true |
Yes |
chattable.sendMessage("Hello world!", "Bot", "robot", false);
chattable.sendPayload(object)
Sends custom JSON data to each connected client, usually used for triggering client-side events or features. Payloads are limited to 500b of data.
chattable.sendPayload({ type: "poke", value: "Alice" });
chattable.setFlair(string)
Updates the user's flair and saves it to localStorage
. Read more about flairs.
chattable.setFlair("flames");
chattable.setName(string)
Updates the user's name on Chattable. This will trigger a popup dialogue from the website asking permission from the user first.
chattable.setName("Andrew");
chattable.minimize()
Minimizes the iframe with a smooth animation. Useful for toggling visibility or embedding collapsible interfaces.
chattable.maximize()
Restores the iframe to its previous height and style after minimizing.
chattable.changeRoom(chat_id)
Switches to a different chatroom by updating the iframe src
.
chattable.changeRoom("123");
chattable.loadStyle(source)
(async)Loads a raw CSS file and sends it to the iframe as a style string.
chattable.loadTheme(name)
(async)Loads a named theme from predefined themes (case insensitive) extracted from the demos on the demo page (e.g., retrowave red
, notepad
, etc.).
chattable.on(event, callback)
Registers an event listener to handle real-time events.
chattable.on("message", function(data){ console.log("New message:", data); });
message
eventThe message
event fires a callback function every time a new message is received in the chat. The callback function is passed an object with the following keys:
Name | Type | Description |
---|---|---|
name |
String | The sender's display name |
flair |
String | The sender's current flair (if set) |
uid |
String | The sender's unique identifier |
text |
String | The message body of the incoming message in plaintext |
payload
eventThe payload
event fires a callback function every time a payload is received. The callback function is passed a copy of the data that was carried in the payload. Note that this data can only contain JSON.
connection
eventThe connection
event fires a callback function every time a user connects or disconnects from the chat. The callback function is passed an object filled with the uid & names of each connected user. Here's an example of an object that was passed to the callback function when a new user joined the chat:
{ "824459503": "Anonymous#1355", "123456789": "John Doe", // etc... }
load
eventThe load
event fires a callback function once the chat is loaded & all data is initialized. This is when other saved variables like chattable.user.name
become available for use.
chattable.off(event, callback)
Removes an event listener that was previously added via on
.
chattable.user
An object containing information about the current user. This object is automatically populated when the chat initializes.
Name | Type | Description |
---|---|---|
name |
String | The user's display name in the chat |
flair |
String | The user's current flair icon (if set) |
uid |
String | The user's unique identifier (populated after initialization) |
console.log(chattable.user.name); // Displays the current user's name
The user object persists the flair in localStorage if set via chattable.setFlair()
.
chattable.commands
An object where each property is a command name that can be triggered from the chat using the !commandName
syntax. Each command is defined as a function that receives the full command text as its parameter.
Important: Commands must be set before calling chattable.initialize()
to ensure they are properly registered with the chat iframe.
// Define commands before initialization chattable.commands = { "poke": function(fullCommand, data) { // Extract the name from something like "!poke Alice" let name = fullCommand.replace(/^\!poke\s/i, ""); chattable.sendPayload({ type: "poke", value: name }); }, "help": function() { // Command implementation } }; // Then initialize the chat chattable.initialize({ stylesheet: "/style.css" });
Command callback functions receive two parameters:
Name | Type | Description |
---|---|---|
fullMessage |
String | The complete message sent by the user, including the command prefix (e.g., "!poke Alice") |
data |
Object | The full packet returned by the API, containing:
|
// Remember: Define commands before initialization chattable.commands = { "login": function(fullCommand, data) { if(data.isMod){ // if the sender is a moderator, // perform the task. } } }; chattable.initialize({ stylesheet: "/style.css" });
When users interact with commands in the chat:
!commandName
in the chat, the iframe detects this as a commandpostMessage
chattable.commands
, its function is executedCommands and payloads work together to create interactive features. Here's a complete example of a "poke" feature:
// Set up command handler chattable.commands = { "poke": function(fullCommand) { let name = fullCommand.replace(/^\!poke\s/i, ""); chattable.sendPayload({ type: "poke", value: name }); } }; // Set up payload handler chattable.on("payload", function(data) { switch(data.type) { case "poke": if(data.value == chattable.user.name) { alert("You have been poked!"); } else { alert(data.value + " got poked!"); } break; default: break; } }); // Initialize chat (after setting commands) chattable.initialize({ theme: "retrowave red" });
chattable.commands
before calling chattable.initialize()
. Commands defined after initialization may not be properly registered until a manual reinitialize()
call.chattable
for the library to function properly.stylesheet
or theme
parameter to initialize()
.