Faye to ActionCable: broadcasting modernization #1

Merged
bterkuile merged 16 commits from feat/review-improve-and-document-the-broadcasting-model into master 2026-05-17 21:23:37 +02:00
Collaborator

Summary

Replaces the Faye real-time broadcasting system with Rails built-in ActionCable, and the DrbCounter with Redis.

mozo-backend

  • ActionCable adapter (Mozo::Broadcaster::ActionCable) — drop-in Faye replacement
  • Redis counter adapter (Mozo::Counter::Redis) — replaces in-memory DrbCounter
  • Broadcastable concern — clean model broadcasting, removes ApplicationController.new anti-pattern
  • Full ActionCable setup: connection, channel, cable.yml, routes
  • Redis adapter for dev and production (shared pub/sub)
  • Employee to Supplier auth flow for mozo-supplier WebSocket connections

mozo-user / mozo-supplier

  • ActionCable Ember service replacing bower faye-browser
  • CoffeeScript fixes: comma bug, @ scoping
  • Channel mapping: /user/123 to user_123, /supplier/456 to supplier_456

See cockpit for full debugging log (7 bugs encountered and fixed).

## Summary Replaces the Faye real-time broadcasting system with Rails built-in ActionCable, and the DrbCounter with Redis. ### mozo-backend - ActionCable adapter (Mozo::Broadcaster::ActionCable) — drop-in Faye replacement - Redis counter adapter (Mozo::Counter::Redis) — replaces in-memory DrbCounter - Broadcastable concern — clean model broadcasting, removes ApplicationController.new anti-pattern - Full ActionCable setup: connection, channel, cable.yml, routes - Redis adapter for dev and production (shared pub/sub) - Employee to Supplier auth flow for mozo-supplier WebSocket connections ### mozo-user / mozo-supplier - ActionCable Ember service replacing bower faye-browser - CoffeeScript fixes: comma bug, @ scoping - Channel mapping: /user/123 to user_123, /supplier/456 to supplier_456 See cockpit for full debugging log (7 bugs encountered and fixed).
benclaw added 16 commits 2026-05-17 21:17:08 +02:00
- Add Mozo::Broadcaster::ActionCable as drop-in Faye replacement
- Fix model_broadcast.rb: delegate to Mozo directly instead of
  ApplicationController.new (memory-unsafe anti-pattern)
- Add Broadcastable concern for clean model-side broadcasting
- ActionCable config: async adapter, cable.yml, WebSocket endpoint
- MozoChannel with per-entity authorization (user/supplier/employee)
- Connection auth via auth_token (matches existing auth pattern)
- Mount /cable WebSocket in routes
- Add broadcasting-migration.md with Faye→ActionCable guide
- include Broadcastable in app/models/list.rb
- include Broadcastable in app/models/order.rb
- Remove config/initializers/model_broadcast.rb (ApplicationController.new anti-pattern)
- Broadcasting now goes through Mozo.broadcast_* directly, not via controller hack
- Add Mozo::Counter::Redis with same get/set/incr/decr interface
- Add redis gem (~> 5.0) to Gemfile
- Update cable.yml to use Redis adapter in production (shared with counters)
- Document DrbCounter → Redis migration in broadcasting-migration.md
- Redis installed and running on vmi3300327
- Leave Faye as current broadcaster; both switches are one-line changes

DrbCounter problems solved:
  - In-memory → persistent (RDB + AOF)
  - Single-process DRb → multi-process safe Redis
  - Atomic INCR/DECR across Puma workers
  - One less custom process to manage
- ActionCable::TaggedLoggerProxy crashes with NoMethodError when
  logger is nil (common in apps upgraded from older Rails)
- Add after_initialize hook to guarantee ActionCable.server.config.logger
  falls back to Rails.logger or
- MozoChannel < ApplicationCable::Channel was failing with
  NameError: uninitialized constant ApplicationCable::Channel
- Standard Rails convention requires both connection.rb and channel.rb
- Benjamin standardized on /user_123 in mozo.rb (underscore, no slash)
- Old remap regex ^/user/(.+)$ didn't match /user_123
- Fix: ^/user[/_](.+)$ accepts both separators → user_123
- Log channel remapping and data on every broadcast
- Log warnings when channel format is unknown
- Helps trace whether broadcasts reach ActionCable.server
- mark_helped! gated broadcast on if save, but save returns false
  when needs_help is already false (no dirty attributes in CouchDB)
- Same fix applied to remove_needs_payment!
- Broadcast is the important side effect — save is just persistence
This reverts commit 4ad701c1a5.
- async adapter is in-process — works in Puma request cycle
  but fails from Rails console (no event loop to deliver messages)
- Redis is shared-state, works from any context (console, jobs, requests)
- Dev uses Redis DB 2, separate channel_prefix from production
- Employee authenticates via auth_token, acts on behalf of a Supplier
- Connection now accepts ?supplier_id=ID query param
- identified_by :current_supplier_id added
- MozoChannel#authorized? allows :employee to subscribe to supplier_<id>
  when current_supplier_id matches
bterkuile merged commit 686dc65b9c into master 2026-05-17 21:23:37 +02:00
bterkuile deleted branch feat/review-improve-and-document-the-broadcasting-model 2026-05-17 21:24:07 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: bterkuile/mozo-backend#1