This package provides both a script (for server instances) and an adapter (for client instances) to enable horizontal scaling of Hubot through service discovery.
Chat Provider → Hubot Server (Script) → Multiple Hubot Clients (Adapter)
↓
Service Discovery
↓
Load Balancing
The server instance runs the chat provider adapter AND the service discovery script.
npm install hubot-service-discoveryAdd to your external-scripts.json:
[
"hubot-service-discovery/DiscoveryService.mjs"
]# Service Discovery Server Configuration
HUBOT_DISCOVERY_PORT=3100 # Port for service discovery server
HUBOT_DISCOVERY_STORAGE=./data # Storage directory for event store
HUBOT_DISCOVERY_TIMEOUT=30000 # Heartbeat timeout in ms
# Instance Configuration
HUBOT_SERVICE_NAME=hubot # Service name for registration
HUBOT_INSTANCE_ID=server # Unique instance identifier
HUBOT_HOST=localhost # Host address for this instance
HUBOT_PORT=8080 # Port for this instance
HUBOT_HEARTBEAT_INTERVAL=15000 # Heartbeat interval in ms# Start with your chat provider adapter (e.g., Slack)
HUBOT_DISCOVERY_PORT=3100 hubot -a slackClient instances connect to the server and receive load-balanced messages.
npm install hubot-service-discovery# Required: Service Discovery Server URL
HUBOT_DISCOVERY_URL=ws://your-server:3100
# Instance Configuration
HUBOT_SERVICE_NAME=hubot # Must match server service name
HUBOT_INSTANCE_ID=client-1 # Unique identifier for this client
HUBOT_HOST=localhost # Host where this client runs
HUBOT_PORT=8080 # Port where this client runs
HUBOT_HEARTBEAT_INTERVAL=15000 # Heartbeat interval in ms# Start with the service discovery adapter
HUBOT_DISCOVERY_URL=ws://your-server:3100 \\
HUBOT_INSTANCE_ID=client-1 \\
hubot -a hubot-service-discovery# Terminal 1 - Server with Slack adapter
export HUBOT_SLACK_TOKEN=xoxb-your-token
export HUBOT_DISCOVERY_PORT=3100
export HUBOT_SERVICE_NAME=hubot
export HUBOT_INSTANCE_ID=server
export HUBOT_HOST=192.168.1.100
export HUBOT_PORT=8080
hubot -a slack# Terminal 2 - First client
export HUBOT_DISCOVERY_URL=ws://192.168.1.100:3100
export HUBOT_SERVICE_NAME=hubot
export HUBOT_INSTANCE_ID=client-1
export HUBOT_HOST=192.168.1.101
export HUBOT_PORT=8081
hubot -a hubot-service-discovery# Terminal 3 - Second client
export HUBOT_DISCOVERY_URL=ws://192.168.1.100:3100
export HUBOT_SERVICE_NAME=hubot
export HUBOT_INSTANCE_ID=client-2
export HUBOT_HOST=192.168.1.102
export HUBOT_PORT=8082
hubot -a hubot-service-discoveryWhen using the script (server mode), these commands are available in chat:
hubot discover services- Show all registered serviceshubot discover hubots- Show all registered Hubot instanceshubot discovery status- Show service discovery statushubot brain peers- Show brain peer connections
- User sends message → Chat Provider (Slack, etc.)
- Chat Provider → Server Hubot instance (with chat adapter + script)
- Server Hubot → Processes message OR forwards to available client
- Client Hubot → Processes message and responds
- Response flows back → Server → Chat Provider → User
The server instance automatically load balances messages across healthy client instances using:
- Instance availability: Based on heartbeat status (instances are healthy if heartbeat within timeout window)
- Configurable strategies: Round-robin (default), random, or least-connections
- Instance capabilities: Metadata-based routing considerations
- Automatic failover: Unhealthy instances are excluded automatically
- Round-Robin (default): Distributes messages evenly across all healthy instances in order
- Random: Randomly selects from available healthy instances
- Least-Connections: Routes to the instance with the fewest active connections (based on metadata)
# Set strategy via environment variable
HUBOT_LB_STRATEGY=round-robin # or random, least-connections
# Or change dynamically via chat commands
hubot lb strategy random
hubot lb status
hubot lb reset # Reset round-robin counterCheck service discovery status:
# In your chat
@hubot discovery status
@hubot discover hubotsView logs for connection status, heartbeats, and message routing.
- Client can't connect: Check
HUBOT_DISCOVERY_URLpoints to server - No load balancing: Ensure clients are registering (check heartbeats)
- Messages not routing: Verify
HUBOT_SERVICE_NAMEmatches across instances - Connection drops: Check network connectivity and firewall settings
HUBOT_LOG_LEVEL=debug hubot -a <adapter>This will show detailed service discovery activity including connections, registrations, and message routing.