OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Django and WebSocket in production

  • Thread starter Thread starter Developer
  • Start date Start date
D

Developer

Guest
I'm working with real-time updates in my Django project for the first time, and it works well locally, but I have an error in production (Microsoft Azure).

Note: I read a couple of articles about using Redis instead of InMemoryChannelLayer.

The Error​


Code:
WebSocket connection to 'wss://mydomain.com/ws/orders/' failed: 
(anonymous) @ orders/:288

The Code​


index.html

Code:
<script type="text/javascript">
    const orderId = '{{ order.id }}';
   let url = `wss://${window.location.host}/ws/orders/`;
   
   const orderSocket = new WebSocket(url)

   orderSocket.onmessage = function(e){
       let data = JSON.parse(e.data);

       document.querySelector('.backdrop').classList.remove('d-none');
       setTimeout(() => {
           location.reload();
       }, 3000);

   }
</script>

settings.py

Code:
ALLOWED_HOSTS = ['mydomain.com', '127.0.0.1', 'localhost'] # my domain refers to my website domain


CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",
    },
}


ASGI_APPLICATION = 'core.asgi.application'


INSTALLED_APPS = [
    'channels',
    # other apps...
]

routing.py

Code:
from django.urls import path
from . import consumers

websocket_urlpatterns = [
    path('ws/orders/', consumers.OrdersCustomers.as_asgi()),
]

asgi.py

Code:
import os

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import management.routing


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    'websocket': AuthMiddlewareStack(
        URLRouter(
            management.routing.websocket_urlpatterns
        )
    )
})

Things I've tried:

Code:
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "host": "redis://localhost:6379",
        },
    },
}

But didn't know how to configure the host.
<p>I'm working with real-time updates in my <strong>Django</strong> project for the first time, and it works well locally, but I have an error in production (<strong>Microsoft Azure</strong>).</p>
<p><em>Note:</em> I read a couple of articles about using <strong>Redis</strong> instead of <strong>InMemoryChannelLayer</strong>.</p>
<h2>The Error</h2>
<pre><code>WebSocket connection to 'wss://mydomain.com/ws/orders/' failed:
(anonymous) @ orders/:288
</code></pre>
<h2>The Code</h2>
<p><strong>index.html</strong></p>
<pre><code><script type="text/javascript">
const orderId = '{{ order.id }}';
let url = `wss://${window.location.host}/ws/orders/`;

const orderSocket = new WebSocket(url)

orderSocket.onmessage = function(e){
let data = JSON.parse(e.data);

document.querySelector('.backdrop').classList.remove('d-none');
setTimeout(() => {
location.reload();
}, 3000);

}
</script>
</code></pre>
<p><strong>settings.py</strong></p>
<pre><code>
ALLOWED_HOSTS = ['mydomain.com', '127.0.0.1', 'localhost'] # my domain refers to my website domain


CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer",
},
}


ASGI_APPLICATION = 'core.asgi.application'


INSTALLED_APPS = [
'channels',
# other apps...
]

</code></pre>
<p><strong>routing.py</strong></p>
<pre><code>from django.urls import path
from . import consumers

websocket_urlpatterns = [
path('ws/orders/', consumers.OrdersCustomers.as_asgi()),
]
</code></pre>
<p><strong>asgi.py</strong></p>
<pre><code>import os

from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import management.routing


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(
URLRouter(
management.routing.websocket_urlpatterns
)
)
})

</code></pre>
<p>Things I've tried:</p>
<pre><code>CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"host": "redis://localhost:6379",
},
},
}
</code></pre>
<p>But didn't know how to configure the <em>host</em>.</p>
Continue reading...
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top