Git Product home page Git Product logo

Comments (5)

hagsteel avatar hagsteel commented on June 16, 2024

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.

heratech avatar heratech commented on June 16, 2024

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.

hagsteel avatar hagsteel commented on June 16, 2024

No problem.
I'll close this issue for now, but if anything is unclear just comment or reopen the issue.

from swampdragon.

chogarcia avatar chogarcia commented on June 16, 2024

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.

hagsteel avatar hagsteel commented on June 16, 2024

that means you are submitting a form to a django view

from swampdragon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.