Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group name must be a valid unicode string containing only ASCII alphanumerics, hyphens, or periods

I need help in creating chat rooms. It works like I'm entering into a specific room (like "Python Community", "DjangoDev", etc.).

Packages that I use:

  • Django==1.9.7
  • channels==1.1.8
  • asgi-redis==1.4.3

I use the slug fields of the group name. I filter this slug in Channels find a group and call save every time a new message comes.

But Channels throws an error which says that the name of group is invalid:

(venv) alibek@OverlorD:~/Desktop/my_porject/RedProject$ ./manage.py runworker
2018-03-17 13:59:51,607 - INFO - runworker - Using single-threaded worker.
2018-03-17 13:59:51,608 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
2018-03-17 13:59:51,608 - INFO - worker - Listening on channels chat-messages, http.request, websocket.connect, websocket.disconnect, websocket.receive
Not Found: /home/
Not Found: /favicon.ico
Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/management/commands/runworker.py", line 83, in handle
    worker.run()
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/worker.py", line 151, in run
    consumer_finished.send(sender=self.__class__)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 192, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/message.py", line 105, in send_and_flush
    sender.send(message, immediately=True)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/channels/channel.py", line 88, in send
    self.channel_layer.send_group(self.name, content)
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/asgi_redis/core.py", line 289, in send_group
    assert self.valid_group_name(group), "Group name not valid"
  File "/home/alibek/Desktop/my_porject/venv/local/lib/python2.7/site-packages/asgiref/base_layer.py", line 122, in valid_group_name
    raise TypeError("Group name must be a valid unicode string containing only ASCII alphanumerics, hyphens, or periods.")
TypeError: Group name must be a valid unicode string containing only ASCII alphanumerics, hyphens, or periods.

Backend code:

def chat_room(request, room_name_url):
    room = get_object_or_404(ChatRoom, slug=room_name_url)
    return render(request, 'chat_room.html', {'room': room})

def msg_consumer(message):
    # Save to model
    room_slug = message.content['room']
    room = ChatRoom.objects.filter(slug=room_slug)

    # Broadcast to listening sockets
    Group("chat-%s" % room).send({
        "text": message.content['message'],
    })

# Connected to websocket.connect
@channel_session
@enforce_ordering
def connect(message):

    message.reply_channel.send({"accept": True})
    # Url of room e.g localhost:8000/chat/django
    path = urlsplit(message.content['path'])
    # Returns slug field of room from url, i.e. 'django'
    room = path[2].strip("/chat/").decode('utf-8')
    # Save room in session and add us to the group
    message.channel_session['room'] = room
    message.content['room'] = room
    Group("chat-%s" % room).add(message.reply_channel)

# Connected to websocket.receive
@channel_session
@enforce_ordering
def receive(message):
    # Stick the message onto the processing queue
    Channel("chat-messages").send({
        "room": message.channel_session['room'],
        "message": message['text'],
    })

# Connected to websocket.disconnect
@channel_session
@enforce_ordering
def disconnect(message):
    Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)

Frontend code:

var messages = document.getElementById("messages");
var text = document.getElementById("text");
var button = document.getElementById("send");

var ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
var socket = new ReconnectingWebSocket(
    ws_scheme
    + '://'
    + window.location.host
    + window.location.pathname
);
socket.onmessage = function (e) {
    var message = document.createElement("p");
    var data = JSON.parse(e.data);
    var noQuotes = data.msg.split('"').join('');
    var user = data.user;
    message.innerHTML = user + ': ' + noQuotes;
    console.log(JSON.parse(e.data));
    messages.appendChild(message);
};
button.addEventListener("click", function (event) {
    event.preventDefault();
    var data_to_server = JSON.stringify(text.value);
    socket.send(data_to_server);
    text.value = '';
    text.focus();
});

Also, I don't like using sessions, Django throws an error for the first time entering into the app.

like image 279
nrgx Avatar asked Oct 16 '22 21:10

nrgx


1 Answers

Well, I found the trouble. I used an object instead of a string:

room = ChatRoom.objects.filter(slug=room_slug)

# Broadcast to listening sockets
Group("chat-%s" % room).send({...

Solution:

Group("chat-%s" % room_slug).send({...
like image 171
nrgx Avatar answered Oct 20 '22 00:10

nrgx