Optional PubSub integration for real-time calendar updates.
When a topic is configured, the calendar component subscribes to live updates so all users viewing the same calendar see changes in real time.
Usage
In your LiveView
def mount(_params, _session, socket) do
# Subscribe to calendar updates
PhoenixLiveCalendar.PubSub.subscribe("calendar:my-calendar-id")
{:ok, socket}
end
def handle_info({:phoenix_live_calendar, event, payload}, socket) do
# Handle calendar events — update your events list
case event do
:event_created -> ...
:event_updated -> ...
:event_deleted -> ...
:events_bulk_updated -> ...
end
endBroadcasting changes (from your context modules)
# After creating an event
PhoenixLiveCalendar.PubSub.broadcast("calendar:my-calendar-id", :event_created, new_event)
# After updating
PhoenixLiveCalendar.PubSub.broadcast("calendar:my-calendar-id", :event_updated, updated_event)
# After deleting
PhoenixLiveCalendar.PubSub.broadcast("calendar:my-calendar-id", :event_deleted, %{id: event_id})
# Bulk operations — single broadcast
PhoenixLiveCalendar.PubSub.broadcast("calendar:my-calendar-id", :events_bulk_updated, %{ids: event_ids})Configuration
Set the PubSub server in your config (defaults to your app's PubSub):
config :phoenix_live_calendar, pubsub_server: MyApp.PubSubOr pass it explicitly:
PhoenixLiveCalendar.PubSub.subscribe("topic", pubsub: MyApp.PubSub)
Summary
Functions
Broadcasts a calendar event to all subscribers of a topic.
Broadcasts a calendar event to all subscribers except the sender.
Subscribes the current process to a calendar topic.
Generates a scoped topic string for a calendar.
Unsubscribes the current process from a calendar topic.
Functions
Broadcasts a calendar event to all subscribers of a topic.
The message is sent as {:phoenix_live_calendar, event_type, payload}.
Parameters
topic— The PubSub topicevent_type— An atom describing the event (e.g.,:event_created)payload— The data associated with the eventopts— Options (:pubsubto specify the PubSub server)
Broadcasts a calendar event to all subscribers except the sender.
Useful when the sender already has the updated state and doesn't need to re-process their own broadcast.
Subscribes the current process to a calendar topic.
Options
pubsub— The PubSub server module (default: from config)
Generates a scoped topic string for a calendar.
Examples
iex> PhoenixLiveCalendar.PubSub.topic("my-calendar")
"phoenix_live_calendar:my-calendar"
iex> PhoenixLiveCalendar.PubSub.topic("my-calendar", resource_id: "room-a")
"phoenix_live_calendar:my-calendar:room-a"
Unsubscribes the current process from a calendar topic.