Using django signals in channels consumer classes

前端 未结 3 1536
礼貌的吻别
礼貌的吻别 2021-01-31 11:16

I am trying to develop an auction type system, where a customer makes an order, and then different stores can offer a price for that order.

An interesting part of this

3条回答
  •  感动是毒
    2021-01-31 12:02

    If you want to talk to the consumer from "outside" - in this case, from a model save method - you'll need to use a Channel Layer to talk to it: http://channels.readthedocs.io/en/latest/topics/channel_layers.html

    Essentially, you'll need to:

    • Add the consumer to a Group on startup (probably based on its order ID)
    • Send a message to the Group whenever there's a new OrderOffer with a custom type - e.g. {"type": "order.new_offer", "order_offer_id": 45}
    • Define a handler on the Consumer that handles this - it matches the type name, so in this case it would be def order_new_offer(self, event):
    • In that handler you can then use self.send to talk down the socket (and query the database if you need extra info to send to the client you didn't put into the event message).

    You can see a variant of this in the MultiChat example project: https://github.com/andrewgodwin/channels-examples/tree/master/multichat

提交回复
热议问题