Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.

Commit 4b3215b

Browse files
committed
add: [leave] leaving a channel
1 parent 93bf498 commit 4b3215b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

realtime/channel.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, socket: Socket, topic: str, params: Dict[str, Any] = {}) -> N
4040
self.listeners: List[CallbackListener] = []
4141
self.joined = False
4242
self.join_ref = str(uuid.uuid4())
43-
self.join_msg_ref = str(uuid.uuid4())
43+
self.control_msg_ref = ""
4444

4545
def join(self) -> Channel:
4646
"""
@@ -62,22 +62,23 @@ async def _join(self) -> None:
6262
payload={}, ref=None)
6363
elif self.socket.version == 2:
6464
#[join_reference, message_reference, topic_name, event_name, payload]
65-
join_req = [self.join_ref, self.join_msg_ref, self.topic, ChannelEvents.join, {}]
65+
self.control_msg_ref = str(uuid.uuid4())
66+
join_req = [self.join_ref, self.control_msg_ref, self.topic, ChannelEvents.join, {}]
6667

6768
try:
6869
await self.socket.ws_connection.send(json.dumps(join_req))
6970
except Exception as e:
7071
print(str(e)) # TODO: better error propagation
7172
return
7273

73-
def leave(self) -> Channel:
74+
def leave(self) -> None:
7475
"""
7576
Wrapper for async def _leave() to expose a non-async interface
7677
Essentially gets the only event loop and attempt leaving a topic
77-
:return: Channel
78+
:return: None
7879
"""
7980
loop = asyncio.get_event_loop() # TODO: replace with get_running_loop
80-
loop.run_until_complete(self._join())
81+
loop.run_until_complete(self._leave())
8182
return self
8283

8384
async def _leave(self) -> None:
@@ -86,13 +87,13 @@ async def _leave(self) -> None:
8687
:return: None
8788
"""
8889
if self.socket.version == 1:
89-
join_req = dict(topic=self.topic, event=ChannelEvents.leave,
90+
leave_req = dict(topic=self.topic, event=ChannelEvents.leave,
9091
payload={}, ref=None)
9192
elif self.socket.version == 2:
92-
join_req = [self.join_ref, None, self.topic, ChannelEvents.leave, {}]
93+
leave_req = [self.join_ref, None, self.topic, ChannelEvents.leave, {}]
9394

9495
try:
95-
await self.socket.ws_connection.send(json.dumps(join_req))
96+
await self.socket.ws_connection.send(json.dumps(leave_req))
9697
except Exception as e:
9798
print(str(e)) # TODO: better error propagation
9899
return

realtime/connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,19 @@ async def _listen(self) -> None:
8181

8282
if msg.event == ChannelEvents.reply:
8383
for channel in self.channels.get(msg.topic, []):
84-
if msg.ref == channel.join_msg_ref :
84+
if msg.ref == channel.control_msg_ref :
8585
logging.info(f"Successfully joined {msg.topic}")
8686
continue
8787
else:
8888
for cl in channel.listeners:
8989
if cl.ref in ["*", msg.ref]:
9090
cl.callback(msg.payload)
91+
92+
if msg.event == ChannelEvents.close:
93+
for channel in self.channels.get(msg.topic, []):
94+
if msg.join_ref == channel.join_ref :
95+
logging.info(f"Successfully left {msg.topic}")
96+
continue
9197

9298
for channel in self.channels.get(msg.topic, []):
9399
for cl in channel.listeners:

0 commit comments

Comments
 (0)