Can't wrap my head around routing - docs reference & Github issue inside

31 views
Skip to first unread message

SK

unread,
Dec 19, 2017, 7:40:25 AM12/19/17
to Django users
Hi,

So maybe I'm getting everything wrong, but I'm a little confused with routing. 

Let's say I want to separate the consumers dealing with messages coming from my domain root:


and my `/import/` route:


I have my routing as so:

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),
]


I'm also separating the Javascript files for initiating the Reconnecting Websockets, in each file I set the path accordingly:

root.js:

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

and import.js:

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);


How can I prevent my receive consumer for import (see: consumers.get_face in routing) from getting or even listening to messages on the root (whose receive consumer is consumers.ws_receive), and vice versa?

Right when I open the console, both messages are sent to both consumers. I guess I can parse the path from the messages, but that seems very laborious!


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.

The confusing sentence to me here is "Given you have made a separate channel especially for this, there's no need for the path routing". Does that imply that there is a way for me to avoid customizing the Group name every time? 


  

SK

unread,
Dec 19, 2017, 12:23:38 PM12/19/17
to Django users
Update: I feel like I'm getting closer to solving this issue, but still can't understand one thing.

If I have my routing file like this:

fb_routing = [
 route
("websocket.connect", consumers.connect_face),
 route
("websocket.receive", consumers.get_face),
 route
("websocket.disconnect", consumers.disconnect_face)
]
# notice each terminating "/$" at the end of each route
home_routing
= [
 route
('websocket.connect', consumers.ws_connect, path=r'^/$'),
 route
('websocket.disconnect', consumers.ws_disconnect, path=r'^/$'),
 route
('websocket.receive', consumers.ws_receive, path=r'^/$'),
]

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


However, if my routing.py looks like this:

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


home_routing
= [

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


channel_routing
= [
 # but shouldn't they *already* get terminated here?
 include(home_routing, path=r'^/$'),
 include
(fb_routing, path=r'^/import/$'),
]



Reply all
Reply to author
Forward
0 new messages