How to get current user from a Django Channels web socket packet?
<p>I was following this tutorial: <a href="https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django" rel="nofollow">Finally, Real-Time Django Is Here: Get Started with Django Channels</a>.</p>

<p>I wanted to extend the app by using Django User objects instead of the <code>handle</code> variable. <strong>But how can I get the current user from the received WebSocket packet in my <code>ws_recieve(message)</code> function?</strong></p>

<p>I noticed that both the <code>csrftoken</code> and the first ten digits of <code>sessionid</code> from the web socket packet match a normal HTTP request. Can I get the current user with this information?</p>

<p>For reference the received packet looks like this:</p>

<pre><code>{'channel': &lt;channels.channel.Channel object at 0x110ea3a20&gt;,
 'channel_layer': &lt;channels.asgi.ChannelLayerWrapper object at 0x110c399e8&gt;,
 'channel_session': &lt;django.contrib.sessions.backends.db.SessionStore object at 0x110d52cc0&gt;,
 'content': {'client': ['127.0.0.1', 52472],
             'headers': [[b'connection', b'Upgrade'],
                         [b'origin', b'http://0.0.0.0:8000'],
                         [b'cookie',
                          b'csrftoken=EQLI0lx4SGCpyTWTJrT9UTe1mZV5cbNPpevmVu'
                          b'STjySlk9ZJvxzHj9XFsJPgWCWq; sessionid=kgi57butc3'
                          b'zckszpuqphn0egqh22wqaj'],
                         [b'cache-control', b'no-cache'],
                         [b'sec-websocket-version', b'13'],
                         [b'sec-websocket-extensions',
                          b'x-webkit-deflate-frame'],
                         [b'host', b'0.0.0.0:8000'],
                         [b'upgrade', b'websocket'],
                         [b'sec-websocket-key', b'y2Lmb+Ej+lMYN+BVrSXpXQ=='],
                         [b'user-agent',
                          b'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) '
                          b'AppleWebKit/602.1.50 (KHTML, like Gecko) Version'
                          b'/10.0 Safari/602.1.50'],
                         [b'pragma', b'no-cache']],
             'order': 0,
             'path': '/chat-message/',
             'query_string': '',
             'reply_channel': 'websocket.send!UZaOWhupBefN',
             'server': ['127.0.0.1', 8000]},
 'reply_channel': &lt;channels.channel.Channel object at 0x110ea3a90&gt;}
</code></pre>

<p>You can get access to a <code>user</code> and <code>http_session</code> attributes of your <code>message</code> by changing the decorators in <code>consumers.py</code> to match the docs:</p>

<blockquote>
  <p>You get access to a userâs normal Django session using the <code>http_session decorator</code> - that gives you a <code>message.http_session</code> attribute that behaves just like <code>request.session</code>. You can go one further and use <code>http_session_user</code> which will provide a <code>message.user</code> attribute as well as the session attribute.  </p>
</blockquote>

<p>so the <a href="https://github.com/jacobian/channels-example/blob/master/chat/consumers.py" rel="nofollow"><code>consumers.py</code></a> file in the example should become the following:  </p>

<pre><code>from channels.auth import http_session_user, channel_session_user, channel_session_user_from_http  

@channel_session_user_from_http  
def ws_connect(message):  
    ...  
    ...


@channel_session_user  
def ws_receive(message):  
    # You can check for the user attr like this  
    log.debug('%s', message.user)  
    ...  
    ...  


@channel_session_user  
def ws_disconnect(message):  
    ...  
    ...  
</code></pre>

<p><strong>Notice the decorators change</strong>.<br>
Also, i suggest you don't build anything upon that example</p>

<p>for more details see: <a href="https://channels.readthedocs.io/en/stable/getting-started.html#authentication" rel="nofollow">https://channels.readthedocs.io/en/stable/getting-started.html#authentication</a>  </p>

python django sockets session django-channels