For context, everything worked fine on my local environment where I use docker-compose with redis.
I have a react frontend that does a websocket connection to the backend. The websocket connects to a azure caching for redis service. Now I am using SSL for this connection and I am setting the right variables for my CHANNELS_LAYER config.
My docker logging gives me this, so I know I am setting the right env's:
{'default': {'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': {'hosts': [('rediss://name.redis.cache.windows.net', 6380, 'REDDIS_PASS')]}}}
My settings.py:
redis_host = os.environ.get('REDIS_HOST', 'localhost')
redis_port = int(os.environ.get('REDIS_PORT', 6379))
redis_password = os.environ.get('REDIS_PASSWORD', '')
use_ssl = os.environ.get('USE_SSL', 'False') == 'True'
redis_scheme = 'rediss' if use_ssl else 'redis'
redis_url = f"{redis_scheme}://{redis_host}"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [(
redis_url,
redis_port,
redis_password
)],
},
},
}
Now my frontend does a ws connection like this:
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//name-api.azurewebsites.net/ws/endpointurl/`;
const ws = new WebSocket(wsUrl);
ws.onmessage = event => {
const message = JSON.parse(event.data);
switch (message.type) {
case 'send_progress_update':
setProgress(message.progress);
console.log(newTabIndex);
if (message.progress === 100) {
if (
(grids.length === 1 && newTabIndex === null) ||
newTabIndex === 0
) {
fetchGrids(analysisId ? analysisId : '');
} else if (newTabIndex !== null) {
setGrids(currentGrids =>
currentGrids.map((grid, index) => {
if (index === newTabIndex) {
return { ...grid, ...message.result, isCompleted: true };
}
return grid;
})
);
}
setNewTabIndex(null);
setIsLoading(false);
}
break;
default:
console.log('Unhandled message', message);
}
};
The error I get in my frontend console is:
index-Zs267d2u.js:188 WebSocket connection to 'wss://name-api.azurewebsites.net/ws/endpointurl/' failed:
My docker logs:
2024-01-31T22:16:14.737634004Z ERROR: Exception in ASGI application
2024-01-31T22:16:14.737690808Z Traceback (most recent call last):
2024-01-31T22:16:14.737697309Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 243, in connect
2024-01-31T22:16:14.737713410Z await self.retry.call_with_retry(
2024-01-31T22:16:14.737718510Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/retry.py", line 59, in call_with_retry
2024-01-31T22:16:14.737723411Z return await do()
2024-01-31T22:16:14.737734212Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 650, in _connect
2024-01-31T22:16:14.737739412Z reader, writer = await asyncio.open_connection(
2024-01-31T22:16:14.737744612Z File "/usr/local/lib/python3.8/asyncio/streams.py", line 52, in open_connection
2024-01-31T22:16:14.737749813Z transport, _ = await loop.create_connection(
2024-01-31T22:16:14.737754413Z File "uvloop/loop.pyx", line 1978, in create_connection
2024-01-31T22:16:14.737759113Z socket.gaierror: [Errno -2] Name or service not known
2024-01-31T22:16:14.737763814Z
2024-01-31T22:16:14.737768114Z During handling of the above exception, another exception occurred:
2024-01-31T22:16:14.737772814Z
2024-01-31T22:16:14.737777215Z Traceback (most recent call last):
2024-01-31T22:16:14.737781815Z File "/usr/local/lib/python3.8/site-packages/channels/utils.py", line 50, in await_many_dispatch
2024-01-31T22:16:14.737786815Z await dispatch(result)
2024-01-31T22:16:14.737791216Z File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 479, in __call__
2024-01-31T22:16:14.737795916Z ret: _R = await loop.run_in_executor(
2024-01-31T22:16:14.737800316Z File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
2024-01-31T22:16:14.737805017Z result = self.fn(*self.args, **self.kwargs)
2024-01-31T22:16:14.737809617Z File "/usr/local/lib/python3.8/site-packages/channels/db.py", line 13, in thread_handler
2024-01-31T22:16:14.737814317Z return super().thread_handler(loop, *args, **kwargs)
2024-01-31T22:16:14.737819018Z File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 538, in thread_handler
2024-01-31T22:16:14.737823618Z return func(*args, **kwargs)
2024-01-31T22:16:14.737828118Z File "/usr/local/lib/python3.8/site-packages/channels/consumer.py", line 125, in dispatch
2024-01-31T22:16:14.737832919Z handler(message)
2024-01-31T22:16:14.737837419Z File "/usr/local/lib/python3.8/site-packages/channels/generic/websocket.py", line 38, in websocket_connect
2024-01-31T22:16:14.737842220Z self.connect()
2024-01-31T22:16:14.737846720Z File "/usr/src/app/appname_core/consumers.py", line 10, in connect
2024-01-31T22:16:14.737851420Z async_to_sync(self.channel_layer.group_add)(self.group_name, self.channel_name)
2024-01-31T22:16:14.737856121Z File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 277, in __call__
2024-01-31T22:16:14.737860921Z return call_result.result()
2024-01-31T22:16:14.737871122Z File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 437, in result
2024-01-31T22:16:14.737875922Z return self.__get_result()
2024-01-31T22:16:14.737880322Z File "/usr/local/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
2024-01-31T22:16:14.737885023Z raise self._exception
2024-01-31T22:16:14.737889423Z File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 353, in main_wrap
2024-01-31T22:16:14.737894023Z result = await self.awaitable(*args, **kwargs)
2024-01-31T22:16:14.737898524Z File "/usr/local/lib/python3.8/site-packages/channels_redis/core.py", line 504, in group_add
2024-01-31T22:16:14.737903224Z await connection.zadd(group_key, {channel: time.time()})
2024-01-31T22:16:14.737907824Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/client.py", line 601, in execute_command
2024-01-31T22:16:14.737912525Z conn = self.connection or await pool.get_connection(command_name, **options)
2024-01-31T22:16:14.737917025Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 1040, in get_connection
2024-01-31T22:16:14.737921725Z await self.ensure_connection(connection)
2024-01-31T22:16:14.737926226Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 1062, in ensure_connection
2024-01-31T22:16:14.737931026Z await connection.connect()
2024-01-31T22:16:14.737935626Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 251, in connect
2024-01-31T22:16:14.737940327Z raise ConnectionError(self._error_message(e))
2024-01-31T22:16:14.737944727Z redis.exceptions.ConnectionError: Error -2 connecting to rediss://name.redis.cache.windows.net:6380. -2.
2024-01-31T22:16:14.737949427Z
2024-01-31T22:16:14.737953728Z During handling of the above exception, another exception occurred:
2024-01-31T22:16:14.737958428Z
2024-01-31T22:16:14.737962728Z Traceback (most recent call last):
2024-01-31T22:16:14.745749503Z File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 255, in run_asgi
2024-01-31T22:16:14.745788806Z result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
2024-01-31T22:16:14.745794606Z File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
2024-01-31T22:16:14.745799507Z return await self.app(scope, receive, send)
2024-01-31T22:16:14.745804107Z File "/usr/local/lib/python3.8/site-packages/channels/routing.py", line 62, in __call__
2024-01-31T22:16:14.745808807Z return await application(scope, receive, send)
2024-01-31T22:16:14.745820108Z File "/usr/local/lib/python3.8/site-packages/channels/sessions.py", line 47, in __call__
2024-01-31T22:16:14.745824909Z return await self.inner(dict(scope, cookies=cookies), receive, send)
2024-01-31T22:16:14.745831509Z File "/usr/local/lib/python3.8/site-packages/channels/sessions.py", line 263, in __call__
2024-01-31T22:16:14.745847110Z return await self.inner(wrapper.scope, receive, wrapper.send)
2024-01-31T22:16:14.745851811Z File "/usr/local/lib/python3.8/site-packages/channels/auth.py", line 185, in __call__
2024-01-31T22:16:14.745856311Z return await super().__call__(scope, receive, send)
2024-01-31T22:16:14.745860711Z File "/usr/local/lib/python3.8/site-packages/channels/middleware.py", line 24, in __call__
2024-01-31T22:16:14.745865312Z return await self.inner(scope, receive, send)
2024-01-31T22:16:14.745869812Z File "/usr/local/lib/python3.8/site-packages/channels/routing.py", line 116, in __call__
2024-01-31T22:16:14.745874412Z return await application(
2024-01-31T22:16:14.745878913Z File "/usr/local/lib/python3.8/site-packages/channels/consumer.py", line 94, in app
2024-01-31T22:16:14.745883513Z return await consumer(scope, receive, send)
2024-01-31T22:16:14.745887913Z File "/usr/local/lib/python3.8/site-packages/channels/consumer.py", line 58, in __call__
2024-01-31T22:16:14.745892414Z await await_many_dispatch(
2024-01-31T22:16:14.745896914Z File "/usr/local/lib/python3.8/site-packages/channels/utils.py", line 57, in await_many_dispatch
2024-01-31T22:16:14.745901514Z await task
2024-01-31T22:16:14.745905815Z File "/usr/local/lib/python3.8/site-packages/channels_redis/core.py", line 353, in receive
2024-01-31T22:16:14.745910415Z message_channel, message = await self.receive_single(
2024-01-31T22:16:14.745914915Z File "/usr/local/lib/python3.8/site-packages/channels_redis/core.py", line 408, in receive_single
2024-01-31T22:16:14.745919516Z content = await self._brpop_with_clean(
2024-01-31T22:16:14.745923916Z File "/usr/local/lib/python3.8/site-packages/channels_redis/core.py", line 244, in _brpop_with_clean
2024-01-31T22:16:14.745928616Z await connection.eval(cleanup_script, 0, channel, backup_queue)
2024-01-31T22:16:14.745933217Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/client.py", line 601, in execute_command
2024-01-31T22:16:14.745937717Z conn = self.connection or await pool.get_connection(command_name, **options)
2024-01-31T22:16:14.745942217Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 1040, in get_connection
2024-01-31T22:16:14.745946918Z await self.ensure_connection(connection)
2024-01-31T22:16:14.745951318Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 1062, in ensure_connection
2024-01-31T22:16:14.745956018Z await connection.connect()
2024-01-31T22:16:14.745960319Z File "/usr/local/lib/python3.8/site-packages/redis/asyncio/connection.py", line 251, in connect
2024-01-31T22:16:14.745964919Z raise ConnectionError(self._error_message(e))
2024-01-31T22:16:14.746969093Z redis.exceptions.ConnectionError: Error -2 connecting to rediss://name.redis.cache.windows.net:6380. -2.
2024-01-31T22:16:14.746985294Z INFO: connection open
2024-01-31T22:16:14.748710322Z INFO: connection closed
In my settings.py I also allowed the redis host so I don't think it's a cors issue.
For anyone having the same problem, i just used my secondary password instead of primary. If anyone knows why i can't use the primary that would be helpfull to understand my architecture.
Also i used this url eventually, with the password being my secondary password on azure redis: rediss://:{secrets.AZURE_REDIS_PASSWORD}@<my_redis_service_name>.redis.redis.cache.windows.net:6380/0