diff --git a/src/actioncable/utils.py b/src/actioncable/utils.py index 4030ed6..cf8ef32 100644 --- a/src/actioncable/utils.py +++ b/src/actioncable/utils.py @@ -17,3 +17,19 @@ def cable_broadcast(group_name: str, message: Union[str, dict]): "message": message, }, ) + + +async def async_cable_broadcast(group_name: str, message: Union[str, dict]): + """ + Rails: ActionCable.server.broadcast + """ + channel_layer = get_channel_layer() + await channel_layer.group_send( + group_name, + { + "type": "action_cable_message", + # should put group_name in the message, so the ActionCableConsumer can know which group this message sent to + "group": group_name, + "message": message, + }, + ) diff --git a/tests/test_consumer.py b/tests/test_consumer.py index 2dcab46..7fcfd6f 100644 --- a/tests/test_consumer.py +++ b/tests/test_consumer.py @@ -2,6 +2,7 @@ from channels.testing import WebsocketCommunicator from channels.layers import get_channel_layer from actioncable import cable_channel_register, ActionCableConsumer, CableChannel, compact_encode_json +from actioncable.utils import async_cable_broadcast @cable_channel_register @@ -45,17 +46,7 @@ async def test_subscribe(): assert response['type'] == 'confirm_subscription' # Message - channel_layer = get_channel_layer() - await channel_layer.group_send( - group_name, - { - "type": "message", - "group": group_name, - "data": { - "message": 'html_snippet', - }, - }, - ) + await async_cable_broadcast(group_name, 'html_snippet') response = await communicator.receive_json_from(timeout=5) assert response['message'] == 'html_snippet' @@ -72,17 +63,7 @@ async def test_subscribe(): await communicator.send_to(text_data=compact_encode_json(subscribe_command)) # Message - channel_layer = get_channel_layer() - await channel_layer.group_send( - group_name, - { - "type": "message", - "group": group_name, - "data": { - "message": 'html_snippet', - }, - }, - ) + await async_cable_broadcast(group_name, 'html_snippet') assert await communicator.receive_nothing() is True