Comments (5)
Hi.
I have just released a new version, so the chat example should now work with Django 1.7 without any issues.
I did not manage to get a CSRF error, could you please elaborate on your setup so I can try to replicate this (if it's still an issue)?
As per SwampDragons design you can not call send
multiple times from the router,
however you can subscribe each user to their own channel and publish to that channel whenever you want to.
If you tell me what you want to achieve in a bit more detail I might be able to propose more relevant solutions.
Why you can't call send
multiple times:
When the client calls a router, it includes a callback name that is sent back by the server.
This is so the client knows what the server is responding to.
Since SwampDragon is async it doesn't call the router and wait for a response, but rather calls the router and goes about it's business.
When the router is ready to respond to the client it will send a message to the client including the callback name.
Once the client receives a message with the appropriate callback name it discards that callback name as being used and moves on.
Imagine the following scenario:
var johns = [];
var janes = [];
function getPeopleNamedJohn() {
window.swampDragon.get_list('people', {name: 'john'}, 'john_callback');
}
function getPeopleNamedJane() {
window.swampDragon.get_list('people', {name: 'jane'}, 'jane_callback');
}
window.swampDragon.on('john_callback', function(context, response) {
johns = response.data;
});
window.swampDragon.on('jane_callback', function(context, response) {
janes = response.data;
});
Now, if we discard the callback name in all this, and let the router respond to the client without any order,
you would not know if you were receiving a list of Johns or a list of Janes
What you can (and should do)
If you want to send messages to individual clients you can subscribe each client to it's own channel.
You can use the session_id
on the session (it's on the connection)
If we create a hypothetical MessageRouter:
class MessageRouter(BaseRouter):
valid_verbs = ['subscribe', 'send_two_messages']
...
def _get_channel_name(self):
channel_name = 'user_channel_{}'.format(self.connection.session.session_id)
return channel_name
def get_subscription_channels(self, **kwargs):
channel = self._get_channel_name()
return [channel]
def send_two_messages(self, **kwargs):
channel = self._get_channel_name()
self.send({'some_data': 'a value'})
self.publish([channel], {'some_data': 'another value'})
Before you call send_two_messages
make sure you call subscribe
so the client is subscribed to the correct channel before any verbs are called (send_two_messages
is a verb).
The send
call will respond to the client when the client calls send_two_messages
.
This is followed by a broadcast message (however in this case we only broadcast to one specific client).
On a side note: remember to add your verbs to valid_verbs
(this is a security measure).
Alternatively you can ...
As I don't know anything about your project I can only make guesses here but:
Because the message footprint is very small (unlike http requests they have no headers or such) and you have an open connection, calling a router twice is still smaller than making one web request.
If we look at a typical request / response header
Request
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:_ga=GA1.2.1320287023.1413234749; _gat=1
Host:my.website.com
If-None-Match:W/"5ef7-2499912417"
Referer:http://my.website.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
Size: 456 chars
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:0
Date:Mon, 13 Oct 2014 21:12:41 GMT
Etag:W/"5ef7-2499912417"
Server:SomeServer
Vary:X-HTTP-Method-Override
X-Powered-By:SomeCoolStuff
Size: 201
You can see that the total size of just the request / response headers are 757 characters.
Since a call to a router and a response from the server, depending on the data, is probably not more than around 20 characters.
This means that in the request / response scenario, making one request that would return the JSON {'key': 'val'} would result in
757 + 14 (the json data is 14 characters) = 761 characters.
If you called a SwampDragon router that returned the same data, it would probably be more in the realms of 40 characters.
Also note that you wouldn't have to establish a connection, send any cookies etc.
So in short: making two calls to your router is still significantly smaller than making just one web request (or five).
However it all depends if it fits your solution or not
from swampdragon.
Reading your answer now. Just want to say before I forget - Thank You! Amazing response. Really helpful and detailed. Thank you so much for your help.
from swampdragon.
No problem.
I'll close this issue for now, but if anything is unclear just comment or reopen the issue.
from swampdragon.
I am also getting CSRF verification failed. Request aborted.
with the chat example.
Just trying submitting some messages on different windows and it is saying CSRF token missing or incorrect.
from swampdragon.
that means you are submitting a form to a django view
from swampdragon.
Related Issues (20)
- how to implement swampdragon properly
- ERROR:tornado.general:WebSocket ... DoesNotExist: TodoList matching query does not exist.
- Settings.js HOT 3
- Synchronize Swampdragon in django cluster
- [frontend] How to handle arriving messages published to concrete channel HOT 6
- 0.4.2.2 issues w/ tutorial psutil HOT 7
- Todo tutorial - route not found HOT 2
- Idea: add template tag to make template parts update in realtime HOT 7
- has_related_value HOT 1
- Release version does not work with Django1.9 HOT 3
- settings.js not found HOT 2
- unit test does not support for auth
- How to avoid 'false' SwampDragon messages
- takeover
- Site can't be reached!? HOT 6
- Connection Swampdragon to a Redis Socket HOT 1
- About swampdragon.net HOT 1
- swampdragon.net site is down HOT 1
- I will to take this site maintanance and also to update it
- Private chat implementation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swampdragon.