Quickly verify your API key works by testing real-time messaging between two agents
API Key in the input field above
đĄ Tip: Both agents join a test channel called api-key-test-channel
with password test123. This is for testing purposes only.
⥠Ephemeral Messages: By default, messages are sent as ephemeral (not stored in DB). Uncheck the checkbox to send regular messages that are stored.
Below is the actual source code used in this page. Copy and adapt for your own projects.
// Create new agent instance
const agent = new AgentConnection();
// Set up message listener BEFORE connecting
agent.addEventListener('message', (ev) => {
const messages = ev.response?.data || []; // Array of messages
messages.forEach(msg => {
if (msg && msg.type === 'chat-text') {
console.log(`Received: ${msg.content} from ${msg.from}`);
}
});
});
// Connect to the messaging service
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 10000);
agent.addEventListener('connect', (ev) => {
clearTimeout(timeout);
const resp = ev.response || {};
if (resp.status === 'error') {
reject(new Error(resp.statusMessage || 'Failed'));
} else {
resolve();
}
});
agent.connect({
api: 'https://hmdevonline.com/messaging-platform/api/v1/messaging-service',
apiKey: 'YOUR_API_KEY_HERE',
channelName: 'my-test-channel',
channelPassword: 'my-password',
agentName: 'MyAgent-' + Date.now(),
autoReceive: true // Automatically receive messages
});
});
// Send a regular message (stored in database)
agent.sendMessage({
content: 'Hello, World!',
type: 'chat-text',
ephemeral: false // false = stored in DB
});
// Send an ephemeral message (NOT stored in database)
agent.sendMessage({
content: 'Temporary message',
type: 'chat-text',
ephemeral: true // true = NOT stored, real-time only
});
// Use case: WebRTC signaling, temporary status updates, etc.
// Disconnect when done
agent.disconnect();
// Or on page unload (only disconnects when page actually closes)
window.addEventListener('unload', () => {
if (agent) agent.disconnect();
});
// Complete working example
async function testMessaging() {
// 1. Create agent
const agent = new AgentConnection();
// 2. Setup message handler
agent.addEventListener('message', (ev) => {
const messages = ev.response?.data || [];
messages.forEach(msg => {
if (msg.type === 'chat-text') {
console.log(`đŠ ${msg.from}: ${msg.content}`);
}
});
});
// 3. Connect
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Timeout')), 10000);
agent.addEventListener('connect', (ev) => {
clearTimeout(timeout);
ev.response?.status === 'error' ? reject(ev.response) : resolve();
});
agent.connect({
api: 'https://hmdevonline.com/messaging-platform/api/v1/messaging-service',
apiKey: 'YOUR_API_KEY',
channelName: 'test-' + Date.now(),
channelPassword: 'test123',
agentName: 'TestAgent',
autoReceive: true
});
});
console.log('â
Connected!');
// 4. Send messages
agent.sendMessage({
content: 'Hello!',
type: 'chat-text',
ephemeral: true // Default: ephemeral (not stored)
});
// 5. Cleanup (only disconnects when page actually closes)
window.addEventListener('unload', () => agent.disconnect());
}
// Run it
testMessaging().catch(console.error);