// Socket.IO v1.3.0 is VERY old (2015). If possible, upgrade to v4.x
// But here's code that should work with both old and new versions:
const SOCKET_URL = 'http://localhost:3000';
// 1. Connect to Socket.IO
const socket = io(SOCKET_URL, {
// For v1.3.0 compatibility
'force new connection': true,
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionAttempts': 5,
'timeout': 20000,
'transports': ['polling', 'websocket']
});
// Your IDs (make sure these match exactly)
const entityId = 'your-extension-entity-id';
const roomId = 'your-room-id'; // This should match the agent/channel ID
// 2. CRITICAL: Join the room when connected
socket.on('connect', function() {
console.log('[SUCCESS] Connected to Eliza, socket ID:', socket.id);
// JOIN THE ROOM - This is required to receive broadcasts!
socket.emit('message', {
type: 1, // ROOM_JOINING
payload: {
roomId: roomId,
entityId: entityId
}
});
console.log('[SENT] Room join request for room:', roomId);
});
// 3. LISTEN FOR THE CORRECT EVENT: "messageBroadcast" (not "message")
socket.on('messageBroadcast', function(data) {
console.log('[RECEIVED] Broadcast:', data);
// Check if this message is for your room
if (data.roomId === roomId || data.channelId === roomId) {
console.log('[SUCCESS] Message is for our room!');
console.log('Sender:', data.senderName);
console.log('Text:', data.text);
console.log('Full data:', JSON.stringify(data, null, 2));
} else {
console.log('[ERROR] Message is for different room:', data.roomId || data.channelId);
}
});
// 4. Listen for other important events
socket.on('messageComplete', function(data) {
console.log('[SUCCESS] Message processing complete:', data);
});
socket.on('connection_established', function(data) {
console.log('[SUCCESS] Connection established:', data);
});
// 5. Send a message (make sure format is exact)
function sendMessageToEliza(text) {
const messagePayload = {
type: 2, // SEND_MESSAGE
payload: {
senderId: entityId,
senderName: 'Extension User',
message: text,
roomId: roomId, // Include roomId
messageId: generateUUID(),
source: 'extension',
attachments: [],
metadata: {}
}
};
console.log('[SENDING] Message:', messagePayload);
socket.emit('message', messagePayload);
}
// Helper function for UUID
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// 6. Debug: Log ALL events (remove in production)
const originalEmit = socket.emit;
socket.emit = function() {
console.log('[EMIT] Event:', arguments[0], arguments[1]);
return originalEmit.apply(socket, arguments);
};
// For Socket.IO v1.3.0, use this to catch all events:
const onevent = socket.onevent;
socket.onevent = function(packet) {
console.log('[RECEIVE] Event:', packet.data);
onevent.call(socket, packet);
};
// Connection error handling
socket.on('connect_error', function(error) {
console.error('[ERROR] Connection error:', error);
});
socket.on('disconnect', function(reason) {
console.log('[DISCONNECTED] Reason:', reason);
});
// Test the connection
socket.on('connect', function() {
// Send a test message after 2 seconds
setTimeout(function() {
sendMessageToEliza('Hello from extension!');
}, 2000);
});