How to connect Django web socket with the third-Party Web Sockets?

I want to connect my Django WebSocket with a third-party web socket. This program is one I wrote, and it functions properly. To avoid having to re-login with the third-party API, I have now added the code to check whether the same room is present in my database. if we use the same API KEY to re-connect to the third-party API. It gives the following error:

{"event":"login","status":401,"message":"Connected from another location"}

I want to see if the same cryptocurrency coin is already connected or not. We don't want to logon with the same API KEY once we're connected. I have two issues here:

  • Don't send the login request to that web socket again.
  • Don't send the subscribe request, if the same coin already exists. Let's say BTCUSD already connected and giving me the data. I want to just connect to the next user to same room and get the data on next request.
import websocket
import time
import ssl
from channels.generic.websocket import AsyncWebsocketConsumer
from .models import Room

login = {
    "event": "login",
    "data": {
        "apiKey": "API_KEY",

class CryptoConsumer(AsyncWebsocketConsumer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs) = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})"wss://")

    async def connect(self):
        self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
        self.room_group_name = "crypto_%s" % self.room_name

        # ****** Code Block ******
        subscribe = {
            "event": "subscribe",
            "data": {
                "ticker": self.room_name,
        room = await Room.add(self.room_name)  # Method in to add the user and return True
        if room is False:
            print("The group with the name %s doesn't exist" % self.room_group_name)

        # ****** End Code Block ******

        # Join room group
        await self.channel_layer.group_add(self.room_group_name, self.channel_name)

        await self.accept()

    async def disconnect(self, close_code):
        unsubscribe = {
            "event": "unsubscribe",
            "data": {
                "ticker": self.room_name,
        # Leave room group
        await self.channel_layer.group_discard(self.room_group_name, self.channel_name)

    # Receive message from WebSocket
    async def receive(self, text_data="{'text': 'Dummy Text'}"):
        text_data_json = json.loads(text_data)
        message = text_data_json["message"]
        message = str(
        # Send message to room group
        await self.channel_layer.group_send(
            self.room_group_name, {"type": "chat_message", "message": message}

    # Receive message from room group
    async def chat_message(self, event):
        message = event["message"]

        # Send message to WebSocket
        await self.send(text_data=json.dumps({"message": message}))

Note: Why I want to do this entire step because we don't want the API KEY made public. So, the code coming from the front end will connect to our Django web socket, and then we'll connect to the third-party web socket and return the data that was sent by them.

Back to Top