WebSocket Setup Guide
Learn how to set up WebSocket-based communication using Slack’s Socket Mode for real-time performance.
Table of contents
- Overview
- Slack App Configuration
- Configuration
- Running with WebSocket
- Testing WebSocket Connection
- Connection Management
- Security Considerations
- Migration from HTTP Mode
- Troubleshooting
- Next Steps
Overview
WebSocket Socket Mode provides significant advantages over HTTP Events API:
Performance Benefits
- 75-90% faster response times (50-200ms vs 500-2000ms)
- Real-time bidirectional communication
- Persistent connections with automatic reconnection
- Event-driven architecture eliminates polling overhead
Deployment Benefits
- No public webhook URLs required
- Outbound-only connections enhance security
- Local development friendly (no ngrok needed)
- Simplified networking and firewall configuration
Slack App Configuration
Enable Socket Mode
- Go to your Slack app settings
- Select your Buidl bot app
- Navigate to “Socket Mode” in the sidebar
- Toggle “Enable Socket Mode” to ON
Generate App-Level Token
- Click “Generate an app-level token”
- Enter token name:
buidl-socket-token
- Add the
connections:write
scope - Click “Generate”
- Copy the App-Level Token (starts with
xapp-
)
⚠️ Important: Keep this token secure - it provides app-level access to your Slack workspace.
Configure Event Subscriptions
- Navigate to “Event Subscriptions”
- Enable Events (if not already enabled)
- Remove webhook URL (not needed for Socket Mode)
- Subscribe to Bot Events:
app_mention
- When bot is mentionedmessage.channels
- Messages in channels bot is added tomessage.im
- Direct messages to bot
Bot Token Scopes
Ensure your bot has these OAuth scopes:
app_mentions:read
- Read mentionschannels:history
- Read channel messageschat:write
- Send messagesim:history
- Read direct messagesusers:read
- Get user information
Configuration
Update Environment Variables
Add these variables to your .env
file:
# Socket Mode Configuration
SLACK_APP_TOKEN=xapp-your-app-level-token-here
BOT_USER_ID=U1234567890
# Existing Bot Configuration
SLACK_BOT_TOKEN=xoxb-your-bot-token-here
OPENROUTER_API_KEY=sk-or-your-api-key-here
Get Bot User ID
You can find your bot’s user ID in several ways:
Method 1: Slack API
curl -X GET https://slack.com/api/auth.test \
-H "Authorization: Bearer xoxb-your-bot-token" \
| jq -r '.user_id'
Method 2: Slack App Settings
- Go to “Basic Information” in your app settings
- Find “App Credentials” section
- The Bot User ID is listed there
Method 3: From Slack Client
- Go to your Slack workspace
- Find your bot in the member list
- Click on the bot profile
- The user ID is in the profile URL:
slack://user?team=T123&id=U123456789
Optional Configuration
# Connection Management
SOCKET_PING_INTERVAL=30 # Seconds between ping messages
SOCKET_RECONNECT_ATTEMPTS=5 # Maximum reconnection attempts
SOCKET_RECONNECT_DELAY=5 # Seconds between reconnections
# Performance Tuning
SOCKET_RECEIVE_TIMEOUT=1 # Message receive timeout
SOCKET_SEND_TIMEOUT=5 # Message send timeout
Running with WebSocket
Start Socket Mode
# Primary command (uses Socket Mode by default)
buidl
# Explicit Socket Mode
buidl-socket
Expected Output
=== BUIDL v1.1.0 (Socket Mode) ===
AI-powered dev bot with WebSocket integration
📋 Configuration loaded successfully
🔐 Privacy level: high
🤖 AI enabled: yes
🔌 Initializing Slack Socket Mode...
Connecting to Slack Socket Mode: wss://wss-primary.slack.com/websocket/...
✅ WebSocket connection established
📞 Received hello from Slack
✅ Slack Socket Mode client initialized
🚀 Starting Buidl Socket Mode bot...
💬 Ready to receive messages via WebSocket!
Health Monitoring
Check connection status:
# View connection health
curl http://localhost:8080/socket-status
# Monitor statistics
curl http://localhost:8080/stats | grep -i socket
Testing WebSocket Connection
Test Bot Mention
In your Slack workspace:
@Buidl Bot hello! Testing WebSocket connection.
You should see:
- Instant response (50-200ms latency)
- Real-time message processing in bot logs
- No polling delays
Performance Comparison
Test response times:
# Socket Mode (WebSocket)
time echo "@Buidl Bot quick test" | slack-send
# HTTP Mode (for comparison)
time echo "@Buidl Bot quick test" | slack-send-http
Expected results:
- Socket Mode: 50-200ms
- HTTP Mode: 500-2000ms
Connection Management
Automatic Reconnection
The bot automatically handles:
- Network disconnections with exponential backoff
- Slack service interruptions with retry logic
- Connection health monitoring with ping/pong
Manual Reconnection
If needed, restart the connection:
# Graceful restart
pkill -TERM buidl-socket
buidl-socket
# Or use systemd (if installed as service)
sudo systemctl restart buidl
Connection Troubleshooting
Debug connection issues:
# Enable WebSocket debugging
export WEBSOCKET_DEBUG=1
buidl-socket
# Check connection logs
tail -f ~/.buidl/logs/websocket.log
# Test basic connectivity
websocket-test wss://wss-primary.slack.com/websocket/test
Security Considerations
Outbound-Only Connections
Socket Mode provides enhanced security:
- No open ports - bot initiates all connections
- No webhook endpoints to secure
- Encrypted connections via WSS (WebSocket Secure)
- Token-based authentication with scoped permissions
Token Security
Protect your App-Level Token:
# Store in secure environment file
echo "SLACK_APP_TOKEN=xapp-..." >> ~/.buidl/.env.secure
chmod 600 ~/.buidl/.env.secure
# Use environment variables in production
export SLACK_APP_TOKEN="$(vault kv get -field=slack_app_token secret/buidl)"
Network Security
Configure firewall for outbound-only:
# Allow outbound HTTPS/WSS only
sudo ufw allow out 443/tcp
sudo ufw deny in 443/tcp
# Block unnecessary inbound connections
sudo ufw default deny incoming
sudo ufw default allow outgoing
Migration from HTTP Mode
Gradual Migration
- Test Socket Mode alongside existing HTTP setup
- Verify functionality with Socket Mode enabled
- Monitor performance and stability
- Switch primary mode to Socket Mode
- Disable HTTP webhooks when confident
Rollback Plan
Keep HTTP mode as fallback:
# Quick switch to HTTP mode if needed
buidl-http
# Or update configuration
echo "USE_SOCKET_MODE=false" >> .env
buidl
Configuration Comparison
Setting | Socket Mode | HTTP Mode |
---|---|---|
SLACK_APP_TOKEN | Required | Not used |
BOT_USER_ID | Required | Optional |
SLACK_SIGNING_SECRET | Not used | Required |
Webhook URL | Not needed | Required |
Public endpoint | Not needed | Required |
Troubleshooting
Common Issues
“Invalid App-Level Token”
- Verify token starts with
xapp-
- Check token has
connections:write
scope - Ensure Socket Mode is enabled in app settings
“WebSocket connection failed”
- Check network connectivity
- Verify firewall allows outbound HTTPS (port 443)
- Test with WebSocket debugging enabled
“Bot not responding to mentions”
- Verify
BOT_USER_ID
is correct - Check bot permissions in channels
- Ensure Event Subscriptions are configured
Debug Commands
# Test WebSocket module
buidl-test --websocket
# Validate Socket Mode configuration
buidl-config --validate-socket
# Check connection health
curl http://localhost:8080/socket-health
Next Steps
- Deployment Guide - Deploy Socket Mode to production
- Performance Tuning - Optimize WebSocket performance
- Monitoring Guide - Monitor WebSocket connections
- API Reference - Explore WebSocket API details