3333
3434class AsyncRealtimeChannel :
3535 """
36- ` Channel` is an abstraction for a topic listener for an existing socket connection.
37- Each Channel has its own topic and a list of event-callbacks that responds to messages.
38- Should only be instantiated through `connection.RealtimeClient() .channel(topic)`.
36+ Channel is an abstraction for a topic subscription on an existing socket connection.
37+ Each Channel has its own topic and a list of event-callbacks that respond to messages.
38+ Should only be instantiated through `AsyncRealtimeClient .channel(topic)`.
3939 """
4040
4141 def __init__ (
@@ -52,13 +52,14 @@ def __init__(
5252 :param params: Optional parameters for connection.
5353 """
5454 self .socket = socket
55- self .params = params or RealtimeChannelOptions (
56- config = {
55+ self .params = params or {}
56+ if self .params .get ("config" ) is None :
57+ self .params ["config" ] = {
5758 "broadcast" : {"ack" : False , "self" : False },
5859 "presence" : {"key" : "" },
5960 "private" : False ,
6061 }
61- )
62+
6263 self .topic = topic
6364 self ._joined_once = False
6465 self .bindings : Dict [str , List [Binding ]] = {}
@@ -97,7 +98,7 @@ def on_close(*args):
9798 logger .info (f"channel { self .topic } closed" )
9899 self .rejoin_timer .reset ()
99100 self .state = ChannelStates .CLOSED
100- self .socket .remove_channel (self )
101+ self .socket ._remove_channel (self )
101102
102103 def on_error (payload , * args ):
103104 if self .is_leaving or self .is_closed :
@@ -148,12 +149,16 @@ async def subscribe(
148149 ] = None ,
149150 ) -> AsyncRealtimeChannel :
150151 """
151- Subscribe to the channel.
152+ Subscribe to the channel. Can only be called once per channel instance.
152153
153- :return: The Channel instance for method chaining.
154+ :param callback: Optional callback function that receives subscription state updates
155+ and any errors that occur during subscription
156+ :return: The Channel instance for method chaining
157+ :raises: Exception if called multiple times on the same channel instance
154158 """
155159 if not self .socket .is_connected :
156160 await self .socket .connect ()
161+
157162 if self ._joined_once :
158163 raise Exception (
159164 "Tried to subscribe multiple times. 'subscribe' can only be called a single time per channel instance"
@@ -249,6 +254,10 @@ def on_join_push_timeout(*args):
249254 return self
250255
251256 async def unsubscribe (self ):
257+ """
258+ Unsubscribe from the channel and leave the topic.
259+ Sets channel state to LEAVING and cleans up timers and pushes.
260+ """
252261 self .state = ChannelStates .LEAVING
253262
254263 self .rejoin_timer .reset ()
@@ -269,6 +278,15 @@ def _close(*args):
269278 async def push (
270279 self , event : str , payload : Dict [str , Any ], timeout : Optional [int ] = None
271280 ) -> AsyncPush :
281+ """
282+ Push a message to the channel.
283+
284+ :param event: The event name to push
285+ :param payload: The payload to send
286+ :param timeout: Optional timeout in milliseconds
287+ :return: AsyncPush instance representing the push operation
288+ :raises: Exception if called before subscribing to the channel
289+ """
272290 if not self ._joined_once :
273291 raise Exception (
274292 f"tried to push '{ event } ' to '{ self .topic } ' before joining. Use channel.subscribe() before pushing events"
@@ -350,9 +368,9 @@ def on_broadcast(
350368 """
351369 Set up a listener for a specific broadcast event.
352370
353- :param event: The name of the broadcast event to listen for.
354- :param callback: The callback function to execute when the event is received.
355- :return: The Channel instance for method chaining.
371+ :param event: The name of the broadcast event to listen for
372+ :param callback: Function called with the payload when a matching broadcast is received
373+ :return: The Channel instance for method chaining
356374 """
357375 return self ._on (
358376 "broadcast" ,
@@ -369,13 +387,14 @@ def on_postgres_changes(
369387 filter : Optional [str ] = None ,
370388 ) -> AsyncRealtimeChannel :
371389 """
372- Set up a listener for a specific Postgres changes event.
373-
374- :param event: The name of the Postgres changes event to listen for.
375- :param table: The table name for which changes should be monitored.
376- :param callback: The callback function to execute when the event is received.
377- :param schema: The database schema where the table exists. Default is 'public'.
378- :return: The Channel instance for method chaining.
390+ Set up a listener for Postgres database changes.
391+
392+ :param event: The type of database event to listen for (INSERT, UPDATE, DELETE, or *)
393+ :param callback: Function called with the payload when a matching change is detected
394+ :param table: The table name to monitor. Defaults to "*" for all tables
395+ :param schema: The database schema to monitor. Defaults to "public"
396+ :param filter: Optional filter string to apply
397+ :return: The Channel instance for method chaining
379398 """
380399
381400 binding_filter = {"event" : event , "schema" : schema , "table" : table }
@@ -402,22 +421,24 @@ def on_system(
402421 # Presence methods
403422 async def track (self , user_status : Dict [str , Any ]) -> None :
404423 """
405- Track a user's presence .
424+ Track presence status for the current user .
406425
407- :param user_status: User's presence status.
408- :return: None
426+ :param user_status: Dictionary containing the user's presence information
409427 """
410428 await self .send_presence ("track" , user_status )
411429
412430 async def untrack (self ) -> None :
413431 """
414- Untrack a user's presence.
415-
416- :return: None
432+ Stop tracking presence for the current user.
417433 """
418434 await self .send_presence ("untrack" , {})
419435
420436 def presence_state (self ) -> RealtimePresenceState :
437+ """
438+ Get the current state of presence on this channel.
439+
440+ :return: Dictionary mapping presence keys to lists of presence payloads
441+ """
421442 return self .presence .state
422443
423444 def on_presence_sync (self , callback : Callable [[], None ]) -> AsyncRealtimeChannel :
@@ -457,11 +478,10 @@ def on_presence_leave(
457478 # Broadcast methods
458479 async def send_broadcast (self , event : str , data : Any ) -> None :
459480 """
460- Sends a broadcast message to the current channel.
481+ Send a broadcast message through this channel.
461482
462- :param event: The name of the broadcast event.
463- :param data: The data to be sent with the message.
464- :return: An asyncio.Future object representing the send operation.
483+ :param event: The name of the broadcast event
484+ :param data: The payload to broadcast
465485 """
466486 await self .push (
467487 ChannelEvents .broadcast ,
0 commit comments