Skip to content

Is it possible to use channel to proxy to another upstream server? #1755

Answered by jtremesay-sereema
variable asked this question in Q&A
Discussion options

You must be logged in to vote

Because I needed to proxify a Jupyter Notebook trough a Django app, here my solution

imoprt asyncio
from channels.generic.websocket import AsyncWebsocketConsumer
from websockets import client as ws_client

class AsyncWebsocketProxyConsumer(AsyncWebsocketConsumer):
    async def websocket_connect(self, message):
        self.upstream = await ws_client.connect("ws://localhost:8888/bla/bla/bla")

        asyncio.create_task(self.receive_upstream())

        await super().accept(subprotocol)

    async def disconnect(self, code):
        await self.upstream.close()

        return await super().disconnect(code)

    async def receive(self, text_data=None, bytes_data=None):
        await self.u…

Replies: 5 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by variable
Comment options

You must be logged in to vote
1 reply
@jtremesay-sereema
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1753 on September 22, 2021 13:21.