Channels routing - must parse path to separate between consumers?

23 views
Skip to first unread message

SK

unread,
Dec 19, 2017, 7:40:25 AM12/19/17
to Django users
So I just spent quite an amount of time posting a lengthy question with specifics and examples but Google Groups simply didn't post. 

I'll make this short:

In github issue below Andew writes:

Ah, well that's because you don't have a path key in your message - the routing isn't magical, it just goes off of the contents of the message. websocket.connect and websocket.receive messages include a path key so they can be routed, whereas yours just has a message key.
Given you have made a separate channel especially for this, there's no need for the path routing unless you really want to distinguish it, in which case you should ensure a path is added to the message when it's sent onto the channel.

 Emphasis mine. Does that mean I don't have to parse the path in each of my consumers in order for them to not send a message down to the same message.

Even with this setup, which as I understand should separate between the different consumers per socket:

fb_routing = [
 route
("websocket.connect", consumers.connect_face),
 route
("websocket.receive", consumers.get_face),
 route
("websocket.disconnect", consumers.disconnect_face)
]


channel_routing
= [
 include
(fb_routing, path=r'^/import/$'),
 route
('websocket.connect', consumers.ws_connect),
 route
('websocket.disconnect', consumers.ws_disconnect),
 route
('websocket.receive', consumers.ws_receive),
]


Both consumers are getting called when I send a message from Javascript with the path:

var ws_scheme = window.location.protocol == "http:" ? "ws" : "wss";
var ws_path = ws_scheme + '://' + window.location.host + "/import";
console
.log("Connecting to " + ws_path);
var mysocket = new ReconnectingWebSocket(ws_path);


So as I understand it now, there's no way to prevent both consumers listening to websocket.connect from getting the message, I can only prevent them from sending the wrong message to the wrong group?

All I want to achieve is to have a separate websocket and its set of consumers on `/import` so I don't have to do multiple if clauses.

Andrew Godwin

unread,
Dec 19, 2017, 1:24:59 PM12/19/17
to django...@googlegroups.com
What you describe there seems like a bug - the first include() should be adding path restrictions. What's confusing is that you said _both_ consumers are getting called? As in each event goes to two routing entries? That definitely shouldn't happen.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/91b500bc-fcc9-4d44-b9ee-ef91de9ddd70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

SK

unread,
Dec 20, 2017, 7:50:07 AM12/20/17
to Django users
Yes. That what was indeed happening. Please see a more detailed thread - the one I said Google Groups didn't post. (The update I posted there actually isn't really a solution because I discovered later it doesn't work)

Now I've got it working, but I could *swear* this exact same setup didn't work for me before:

root/routing.py

channel_routing = [
include('core.routing.home_routing', path=r'^/$'),
include('core.routing.fb_routing', path=r'^/import/$'),
]

 

home_routing = [

route('websocket.connect', consumers.ws_connect),
route('websocket.disconnect', consumers.ws_disconnect),
route('websocket.receive', consumers.ws_receive),
]

fb_routing = [
route("websocket.receive", consumers.get_face),
]


Maybe having `websocket.connect` in both lists was the culprit? this issue was *super* confusing. If you want, Andrew, I could re-configure my routing to look as before and send you some details for debugging. I could swear the same setup as above was giving both or only one of the consumers working, but never either as it should.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Dec 20, 2017, 12:02:44 PM12/20/17
to django...@googlegroups.com
If you can replicate consumers being called twice on one event that's a serious issue we need to look into (the only thing that does this by design is the multiplexer, which calls all its sub-consumers for all events).

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages