PubSub event broadcasting for Shop module.
This module provides functions to broadcast cart changes across multiple browser tabs and devices for the same user/session, as well as product, category, and inventory updates for real-time admin dashboards.
Topics
shop:cart:user:{user_id}- Cart events for authenticated usersshop:cart:session:{session_id}- Cart events for guest sessionsshop:products- Product events (created, updated, deleted)shop:categories- Category events (created, updated, deleted)shop:inventory- Inventory events (stock changes)shop:products:{product_id}- Individual product events
Events
Cart Events
{:cart_updated, cart}- Cart totals changed (generic update){:item_added, cart, item}- Item added to cart{:item_removed, cart, item_id}- Item removed from cart{:quantity_updated, cart, item}- Item quantity changed{:shipping_selected, cart}- Shipping method selected/changed{:payment_selected, cart}- Payment option selected/changed{:cart_cleared, cart}- All items removed from cart
Product Events
{:product_created, product}- New product created{:product_updated, product}- Product updated{:product_deleted, product_id}- Product deleted{:products_bulk_status_changed, product_ids, status}- Bulk status update
Category Events
{:category_created, category}- New category created{:category_updated, category}- Category updated{:category_deleted, category_id}- Category deleted
Inventory Events
{:inventory_updated, product_id, stock_change}- Stock level changed
Examples
# Subscribe to cart updates for authenticated user
Events.subscribe_to_user_cart(user_id)
# Subscribe to cart updates for guest session
Events.subscribe_to_session_cart(session_id)
# Subscribe to product updates (admin dashboard)
Events.subscribe_products()
# Broadcast item added
Events.broadcast_item_added(cart, item)
# Broadcast product created
Events.broadcast_product_created(product)
# Handle in LiveView
def handle_info({:item_added, cart, _item}, socket) do
{:noreply, assign(socket, :cart, cart)}
end
Summary
Functions
Broadcasts cart cleared event.
Broadcasts a generic cart update event.
Broadcasts bulk category deleted event.
Broadcasts bulk category parent changed event.
Broadcasts bulk category status changed event.
Broadcasts category created event.
Broadcasts category deleted event.
Broadcasts category updated event.
Broadcasts inventory updated event.
Broadcasts item added event.
Broadcasts item removed event.
Broadcasts payment option selected event.
Broadcasts product created event.
Broadcasts product deleted event.
Broadcasts product updated event.
Broadcasts bulk product status changed event.
Broadcasts quantity updated event.
Broadcasts shipping method selected event.
Returns the appropriate topic(s) for a cart.
Returns the PubSub topic for all categories.
Returns the PubSub topic for inventory events.
Returns the PubSub topic for a specific product.
Returns the PubSub topic for all products.
Returns the PubSub topic for a session's cart.
Subscribes to category events.
Subscribes to inventory events.
Subscribes to events for a specific product.
Subscribes to product events.
Subscribes to cart events for a specific cart. Subscribes to all relevant topics (user and/or session).
Subscribes to cart events for a guest session.
Subscribes to cart events for an authenticated user.
Unsubscribes from cart events for a specific cart.
Unsubscribes from cart events for a guest session.
Unsubscribes from cart events for an authenticated user.
Returns the PubSub topic for a user's cart.
Functions
Broadcasts cart cleared event.
Broadcasts a generic cart update event.
Broadcasts bulk category deleted event.
Broadcasts bulk category parent changed event.
Broadcasts bulk category status changed event.
Broadcasts category created event.
Broadcasts category deleted event.
Broadcasts category updated event.
Broadcasts inventory updated event.
Broadcasts item added event.
Broadcasts item removed event.
Broadcasts payment option selected event.
Broadcasts product created event.
Broadcasts product deleted event.
Broadcasts product updated event.
Broadcasts bulk product status changed event.
Broadcasts quantity updated event.
Broadcasts shipping method selected event.
Returns the appropriate topic(s) for a cart.
Returns the PubSub topic for all categories.
Returns the PubSub topic for inventory events.
Returns the PubSub topic for a specific product.
Returns the PubSub topic for all products.
Returns the PubSub topic for a session's cart.
Subscribes to category events.
Subscribes to inventory events.
Subscribes to events for a specific product.
Subscribes to product events.
Subscribes to cart events for a specific cart. Subscribes to all relevant topics (user and/or session).
Subscribes to cart events for a guest session.
Subscribes to cart events for an authenticated user.
Unsubscribes from cart events for a specific cart.
Unsubscribes from cart events for a guest session.
Unsubscribes from cart events for an authenticated user.
Returns the PubSub topic for a user's cart.