9.1 KiB
9.1 KiB
Live Chat Widget - Implementation Summary
Overview
A comprehensive live chat widget system has been implemented that supports both Twitch and YouTube live chat with extensive emote support (FrankerFaceZ, BetterTTV, 7TV) and real-time WebSocket streaming to OBS.
Architecture
Frontend Components
-
Chat Widget (
app/assets/web/widgets/livechat/)index.html- Minimal HTML structurestyle.css- Styled chat interface with platform indicators, badges, animationsapp.js- WebSocket client, message rendering, emote parsing, auto-scroll
-
Configuration UI (
app/assets/web/config.html)- Platform authentication controls
- Channel/video configuration
- Emote provider toggles
- Display settings
- Real-time status indicators
Backend Components
-
Data Models (
app/chat_models.py)Platform- Enum for Twitch/YouTubeUserRole- Broadcaster, Mod, VIP, Subscriber, ViewerEmote- Code, URL, provider, animation flagChatBadge- Badge name and iconChatUser- User ID, name, color, roles, badgesChatMessage- Complete message with user, text, emotes, timestampAuthTokens- OAuth token storage with expiryChatConfig- All widget configuration options
-
State Management (
app/state.py)- Extended
AppStatewith:chat_messages- Deque of recent messages (max 100)chat_config- Current configurationtwitch_tokens/youtube_tokens- OAuth credentials
- Methods for adding messages and broadcasting to WebSocket clients
- Extended
-
Twitch Integration (
app/providers/twitch_chat.py)- IRC WebSocket client (
wss://irc-ws.chat.twitch.tv:443) - Supports anonymous and authenticated connections
- IRC message parsing (PRIVMSG, tags, badges, emotes)
- Emote loading from:
- Twitch native (from IRC tags)
- FrankerFaceZ API (global + channel)
- BetterTTV API (global + channel)
- 7TV API (global + channel)
- Auto-reconnect on disconnect
- IRC WebSocket client (
-
YouTube Integration (
app/providers/youtube_chat.py)- YouTube Live Chat API polling client
- OAuth required (YouTube Data API v3)
- Fetches live chat ID from video
- Polls for new messages with adaptive interval
- Parses user roles (owner, moderator, sponsor)
-
Authentication System (
app/auth.py)- OAuth flow handlers for Twitch and YouTube
- Token storage in
%LOCALAPPDATA%/StreamerWidgets/tokens.json - Automatic token loading on startup
- Browser-based authentication with popup windows
- Callback URL handling at
/auth/{platform}/callback
-
Chat Manager (
app/chat_manager.py)- Coordinates Twitch and YouTube clients
- Starts/stops based on configuration
- Manages asyncio tasks for each platform
- Graceful shutdown and restart
-
WebServer Updates (
app/webserver.py)- Added livechat widget to WIDGETS list
/api/chat/messages- Get recent messages/api/chat/config- GET/POST configuration/config- Configuration UI page- OAuth routes registered via
register_auth_routes() - Enhanced WebSocket to send chat history on connect
-
Main Integration (
app/main.py)- Load saved OAuth tokens on startup
- Initialize and start ChatManager
- Graceful shutdown of chat connections
Features Implemented
Core Functionality
- ✅ Real-time chat from Twitch and YouTube
- ✅ WebSocket streaming to OBS browser source
- ✅ Unified message stream (both platforms mixed)
- ✅ Platform-specific visual indicators (color borders, icons)
- ✅ User badges (broadcaster, mod, VIP, subscriber)
- ✅ Username colors (Twitch native colors)
- ✅ Timestamps
- ✅ Message animations (slide-in effect)
- ✅ Auto-scroll with manual scroll detection
Emote Support
- ✅ Twitch native emotes
- ✅ FrankerFaceZ (FFZ) global and channel emotes
- ✅ BetterTTV (BTTV) global and channel emotes
- ✅ 7TV global and channel emotes
- ✅ Animated emote support
- ✅ Emote caching
- ✅ Configurable emote provider toggles
Authentication
- ✅ Twitch OAuth (optional, anonymous reading supported)
- ✅ YouTube OAuth (required for YouTube API)
- ✅ Secure token storage
- ✅ Token persistence across restarts
- ✅ Browser-based auth flow
Configuration
- ✅ Web-based configuration UI
- ✅ Platform enable/disable
- ✅ Channel/video ID settings
- ✅ Emote provider toggles
- ✅ Display options (timestamps, badges, max messages)
- ✅ Live status indicators
OBS Integration
- ✅ Transparent background
- ✅ Customizable CSS styling
- ✅ Responsive design
- ✅ Low resource usage
- ✅ Auto-reconnecting WebSocket
API Endpoints
Widget Access
GET /widgets/livechat/- Chat widget HTMLGET /config- Configuration page
REST API
GET /api/chat/messages?limit=50- Fetch recent messagesGET /api/chat/config- Get current configurationPOST /api/chat/config- Update configuration
WebSocket
GET /ws- Real-time message stream- Sends
chat_historyon connect - Sends
chat_messagefor each new message
- Sends
OAuth
GET /auth/twitch/login- Initiate Twitch OAuthGET /auth/twitch/callback- Twitch OAuth callbackGET /auth/youtube/login- Initiate YouTube OAuthGET /auth/youtube/callback- YouTube OAuth callback
File Structure
app/
├── chat_models.py # Data models for chat system
├── chat_manager.py # Chat client coordinator
├── auth.py # OAuth handlers
├── state.py # Extended with chat state
├── webserver.py # Added chat endpoints
├── main.py # Integrated chat manager
├── providers/
│ ├── twitch_chat.py # Twitch IRC client
│ └── youtube_chat.py # YouTube API client
└── assets/web/
├── config.html # Configuration UI
└── widgets/
└── livechat/
├── index.html # Widget HTML
├── style.css # Widget styles
└── app.js # WebSocket client
CHAT_SETUP.md # User setup guide
IMPLEMENTATION_SUMMARY.md # This file
Configuration Requirements
For Twitch (Optional OAuth)
Users who want authenticated Twitch access need to:
- Create app at https://dev.twitch.tv/console/apps
- Set redirect URI to
http://localhost:8765/auth/twitch/callback - Edit
app/auth.pywith Client ID and Secret
Anonymous reading works without OAuth.
For YouTube (Required OAuth)
Users need to:
- Create Google Cloud project
- Enable YouTube Data API v3
- Create OAuth credentials (Web application)
- Set redirect URI to
http://localhost:8765/auth/youtube/callback - Edit
app/auth.pywith Client ID and Secret
Usage Flow
- Start Server:
uv run streamer-widgets --tray - Configure (if using OAuth):
- Visit http://127.0.0.1:8765/config
- Click "Login with Twitch" or "Login with YouTube"
- Authorize in browser popup
- Set Channel/Video:
- Enter Twitch channel name
- Enter YouTube video ID
- Save configuration
- Add to OBS:
- Create Browser Source
- URL:
http://127.0.0.1:8765/widgets/livechat/ - Size: 400x600 (or custom)
- Chat appears in OBS with live updates
Technical Highlights
Twitch IRC
- Efficient IRC WebSocket connection
- Minimal overhead (no polling)
- Supports IRC tags for rich metadata
- Handles PING/PONG keepalive
- Graceful reconnection
YouTube API
- Polling-based with adaptive intervals
- Respects API rate limits
- Extracts live chat ID automatically
- Handles OAuth token refresh
Emote Systems
- Parallel API calls for fast loading
- Caching to avoid repeated requests
- Fallback for missing emotes
- Support for both static and animated
WebSocket Broadcasting
- Efficient message distribution
- Dead client cleanup
- Initial history on connect
- Type-safe message format
Performance
- Message limit (deque with maxlen)
- Auto-scroll optimization
- Lazy emote replacement
- Minimal DOM updates
Future Enhancements
Potential additions (not implemented):
- Separate column view (Twitch | YouTube)
- Message filtering by keywords/roles
- Custom badge images
- Sound notifications
- Chat replay
- Custom themes
- Viewer count display
- Raid/host notifications
- Chat commands
- Donation/sub alerts
Testing Checklist
- Twitch anonymous connection
- Twitch authenticated connection
- YouTube authenticated connection
- FFZ emotes display
- BTTV emotes display
- 7TV emotes display
- User badges display
- Platform indicators
- WebSocket reconnection
- Configuration persistence
- OAuth token storage
- Multiple browser sources
- OBS transparency
- Auto-scroll behavior
- Manual scroll detection
Notes
- All dependencies already present (aiohttp)
- No additional packages required for basic functionality
- OAuth credentials must be user-provided
- Tokens stored locally (not cloud)
- Chat history is in-memory only (not persisted to disk)
- Anonymous Twitch reading has no rate limits
- YouTube API has daily quota (sufficient for chat reading)