How do I make my webhook object run a websocket function?

32 views
Skip to first unread message

Jeremy Wai

unread,
Jan 8, 2020, 11:22:11 PM1/8/20
to Django users
I got a heroku server running on one web processing, in the routing.py I have

    application = ProtocolTypeRouter({
        'websocket': backEndConsumer,
        'http': frontEndConsumer,
    })

my frontEndConsumer handles the http webhook requests and my backEndConsumer handles all my websocket requests.

When a device with the correct address tries to talk to my websocket, it will go to backEndConsumer and run the websocket_connect()
Then on my frontEndconsumer, It will get a http_request().

I want this http_request() to trigger the backEndConsumer to do a websocket_send(). I can't seem to get that to work.

webhook.py
scope = None
import myapp.consumers

    class frontEndConsumer(AsyncHttpConsumer):
        async def http_request(self, request):
            myapp.consumers.backEndConsumer.frontSend(myapp.consumers.scope)
            await self.send_response(200, b"finished",
                headers=[(b"Content-Type", b"text/plain"),])
               
consumers.py

    class backEndConsumer(AsyncJsonWebsocketConsumer):
        async def websocket_connect(self, type):
            await self.accept()
            global scope
            scope = self   # doing this to pass the object to frontEndConsumer
        async def frontSend(self):
            await self.send("Hello world!")
           
I keep getting this error
    myapp.consumers.backEndConsumer.frontSend()
    frontSend() missing 1 required positional argument: 'self'
I know I am doing something wrong, but I am not sure how to do what I want.




Jeremy Wai

unread,
Jan 9, 2020, 11:36:21 PM1/9/20
to Django users
going to answer my own question for the sake of anyone who runs into things while learning python:

I needed channel redis that can pass messages from one process to another. redis is just a key/value storage server that you can install in ubuntu.
Reply all
Reply to author
Forward
0 new messages