This is attempting to send to a channel named “notifications”.
Unless you have a worker process associated with that name, there’s no place to deliver the message to.
If you’re looking to send a message to a specific user, you need to “locate” that user.
The method that I use, is that every user logging on joins a group named user_<username>
. (e.g. user_ken
).
Any other channels client can send a message to me by sending a message to the group user_ken
.
Example from what you have above:
def send_notification(message):
# This will print the message to the console
print(f"Sending message: {message}")
# assume that the target username is in `message.username`
group_name = f'user_{message.username}'
# This will send the message to the client.
async_to_sync(channel_layer.group_send)(group_name, {
'type': 'notification.message',
'message': message
})
Otherwise, you would need to know the precise channel name for that user in order to direct the message to that user.