Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable emitting to single client in managers with to=... #1374

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/socketio/async_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ async def can_disconnect(self, sid, namespace):
return self.is_connected(sid, namespace)

async def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.

Note: this method is a coroutine.
"""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):
Expand Down
3 changes: 2 additions & 1 deletion src/socketio/async_pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(self):
self._get_logger().info(self.name + ' backend initialized.')

async def emit(self, event, data, namespace=None, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.

Expand All @@ -49,6 +49,7 @@ async def emit(self, event, data, namespace=None, room=None, skip_sid=None,

Note: this method is a coroutine.
"""
room = to or room
if kwargs.get('ignore_queue'):
return await super().emit(
event, data, namespace=namespace, room=room, skip_sid=skip_sid,
Expand Down
3 changes: 2 additions & 1 deletion src/socketio/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def can_disconnect(self, sid, namespace):
return self.is_connected(sid, namespace)

def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace."""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):
Expand Down
3 changes: 2 additions & 1 deletion src/socketio/pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def initialize(self):
self._get_logger().info(self.name + ' backend initialized.')

def emit(self, event, data, namespace=None, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.

Expand All @@ -46,6 +46,7 @@ def emit(self, event, data, namespace=None, room=None, skip_sid=None,

The parameters are the same as in :meth:`.Server.emit`.
"""
room = to or room
if kwargs.get('ignore_queue'):
return super().emit(
event, data, namespace=namespace, room=room, skip_sid=skip_sid,
Expand Down
2 changes: 1 addition & 1 deletion tests/async/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def test_emit_to_sid(self):
_run(self.bm.connect('456', '/foo'))
_run(
self.bm.emit(
'my event', {'foo': 'bar'}, namespace='/foo', room=sid
'my event', {'foo': 'bar'}, namespace='/foo', to=sid
)
)
assert self.bm.server._send_eio_packet.mock.call_count == 1
Expand Down
16 changes: 16 additions & 0 deletions tests/async/test_pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ def test_emit(self):
}
)

def test_emit_with_to(self):
sid = 'room-mate'
_run(self.pm.emit('foo', 'bar', to=sid))
self.pm._publish.mock.assert_called_once_with(
{
'method': 'emit',
'event': 'foo',
'data': 'bar',
'namespace': '/',
'room': sid,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)

def test_emit_with_namespace(self):
_run(self.pm.emit('foo', 'bar', namespace='/baz'))
self.pm._publish.mock.assert_called_once_with(
Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_rooms(self):
def test_emit_to_sid(self):
sid = self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo')
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=sid)
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', to=sid)
assert self.bm.server._send_eio_packet.call_count == 1
assert self.bm.server._send_eio_packet.call_args_list[0][0][0] == '123'
pkt = self.bm.server._send_eio_packet.call_args_list[0][0][1]
Expand Down
16 changes: 16 additions & 0 deletions tests/common/test_pubsub_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ def test_emit(self):
}
)

def test_emit_with_to(self):
sid = "ferris"
self.pm.emit('foo', 'bar', to=sid)
self.pm._publish.assert_called_once_with(
{
'method': 'emit',
'event': 'foo',
'data': 'bar',
'namespace': '/',
'room': sid,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)

def test_emit_with_namespace(self):
self.pm.emit('foo', 'bar', namespace='/baz')
self.pm._publish.assert_called_once_with(
Expand Down
Loading