Skip to content

Commit f147604

Browse files
authored
Enable emitting to single client in managers with to=... (miguelgrinberg#1374)
* Enable emitting to single client in AsyncPubSubManager * Handle `to` in async manager and sync versions * Name tests consistently * Rm extra blank line in test_pubsub_manager
1 parent 9f34493 commit f147604

File tree

8 files changed

+42
-6
lines changed

8 files changed

+42
-6
lines changed

src/socketio/async_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ async def can_disconnect(self, sid, namespace):
1111
return self.is_connected(sid, namespace)
1212

1313
async def emit(self, event, data, namespace, room=None, skip_sid=None,
14-
callback=None, **kwargs):
14+
callback=None, to=None, **kwargs):
1515
"""Emit a message to a single client, a room, or all the clients
1616
connected to the namespace.
1717
1818
Note: this method is a coroutine.
1919
"""
20+
room = to or room
2021
if namespace not in self.rooms:
2122
return
2223
if isinstance(data, tuple):

src/socketio/async_pubsub_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize(self):
3838
self._get_logger().info(self.name + ' backend initialized.')
3939

4040
async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
41-
callback=None, **kwargs):
41+
callback=None, to=None, **kwargs):
4242
"""Emit a message to a single client, a room, or all the clients
4343
connected to the namespace.
4444
@@ -49,6 +49,7 @@ async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
4949
5050
Note: this method is a coroutine.
5151
"""
52+
room = to or room
5253
if kwargs.get('ignore_queue'):
5354
return await super().emit(
5455
event, data, namespace=namespace, room=room, skip_sid=skip_sid,

src/socketio/manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ def can_disconnect(self, sid, namespace):
2020
return self.is_connected(sid, namespace)
2121

2222
def emit(self, event, data, namespace, room=None, skip_sid=None,
23-
callback=None, **kwargs):
23+
callback=None, to=None, **kwargs):
2424
"""Emit a message to a single client, a room, or all the clients
2525
connected to the namespace."""
26+
room = to or room
2627
if namespace not in self.rooms:
2728
return
2829
if isinstance(data, tuple):

src/socketio/pubsub_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(self):
3737
self._get_logger().info(self.name + ' backend initialized.')
3838

3939
def emit(self, event, data, namespace=None, room=None, skip_sid=None,
40-
callback=None, **kwargs):
40+
callback=None, to=None, **kwargs):
4141
"""Emit a message to a single client, a room, or all the clients
4242
connected to the namespace.
4343
@@ -46,6 +46,7 @@ def emit(self, event, data, namespace=None, room=None, skip_sid=None,
4646
4747
The parameters are the same as in :meth:`.Server.emit`.
4848
"""
49+
room = to or room
4950
if kwargs.get('ignore_queue'):
5051
return super().emit(
5152
event, data, namespace=namespace, room=room, skip_sid=skip_sid,

tests/async/test_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_emit_to_sid(self):
205205
_run(self.bm.connect('456', '/foo'))
206206
_run(
207207
self.bm.emit(
208-
'my event', {'foo': 'bar'}, namespace='/foo', room=sid
208+
'my event', {'foo': 'bar'}, namespace='/foo', to=sid
209209
)
210210
)
211211
assert self.bm.server._send_eio_packet.mock.call_count == 1

tests/async/test_pubsub_manager.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ def test_emit(self):
6767
}
6868
)
6969

70+
def test_emit_with_to(self):
71+
sid = 'room-mate'
72+
_run(self.pm.emit('foo', 'bar', to=sid))
73+
self.pm._publish.mock.assert_called_once_with(
74+
{
75+
'method': 'emit',
76+
'event': 'foo',
77+
'data': 'bar',
78+
'namespace': '/',
79+
'room': sid,
80+
'skip_sid': None,
81+
'callback': None,
82+
'host_id': '123456',
83+
}
84+
)
85+
7086
def test_emit_with_namespace(self):
7187
_run(self.pm.emit('foo', 'bar', namespace='/baz'))
7288
self.pm._publish.mock.assert_called_once_with(

tests/common/test_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_rooms(self):
206206
def test_emit_to_sid(self):
207207
sid = self.bm.connect('123', '/foo')
208208
self.bm.connect('456', '/foo')
209-
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=sid)
209+
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', to=sid)
210210
assert self.bm.server._send_eio_packet.call_count == 1
211211
assert self.bm.server._send_eio_packet.call_args_list[0][0][0] == '123'
212212
pkt = self.bm.server._send_eio_packet.call_args_list[0][0][1]

tests/common/test_pubsub_manager.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ def test_emit(self):
7878
}
7979
)
8080

81+
def test_emit_with_to(self):
82+
sid = "ferris"
83+
self.pm.emit('foo', 'bar', to=sid)
84+
self.pm._publish.assert_called_once_with(
85+
{
86+
'method': 'emit',
87+
'event': 'foo',
88+
'data': 'bar',
89+
'namespace': '/',
90+
'room': sid,
91+
'skip_sid': None,
92+
'callback': None,
93+
'host_id': '123456',
94+
}
95+
)
96+
8197
def test_emit_with_namespace(self):
8298
self.pm.emit('foo', 'bar', namespace='/baz')
8399
self.pm._publish.assert_called_once_with(

0 commit comments

Comments
 (0)