evilkost / brukva

Asynchronous Redis client that works within Tornado IO loop.
Other
265 stars 33 forks source link

brukva in long polling #13

Closed goldalworming closed 13 years ago

goldalworming commented 13 years ago

I'm trying to make long polling from asynchronous brukva pubsub but got error this error in implement tornado chat example

    class MessageNewHandler(BaseHandler):
        @tornado.web.authenticated
        def post(self):
            message = {
                "id": str(uuid.uuid4()),
                "from": self.current_user["first_name"],
                "body": self.get_argument("body"),
            }
            message["html"] = self.render_string("message.html", message=message)
            if self.get_argument("next", None):
                self.redirect(self.get_argument("next"))
            else:
                self.write(message)
                c = brukva.Client()
                c.connect()
                c.publish('test_channel', message)

    class MessageUpdatesHandler(BaseHandler):
        @tornado.web.authenticated
        @tornado.web.asynchronous

        def post(self):
            self.client = brukva.Client()
            self.client.connect()
            self.client.subscribe('test_channel')
            self.client.listen(self.on_new_messages)

        def on_new_messages(self, messages):
            # Closed client connection
            if self.request.connection.stream.closed():
                return
            self.finish(dict(messages=messages))

but i got these error

            [E 110607 02:44:39 web:900] Uncaught exception POST /a/message/updates (127.0.0.1)
            .
            .
            .
            File "/usr/lib/python2.7/json/encoder.py", line 178, in default
            raise TypeError(repr(o) + " is not JSON serializable")
            TypeError: <brukva.client.Message object at 0x9fafe0c> is not JSON serializable
goldalworming commented 13 years ago

chage def on_new_messages(self, messages):

Closed client connection

    if self.request.connection.stream.closed():
        return
    self.finish(dict(messages=str(messages.body)))

and it's done

burakdede commented 12 years ago

@goldalworming Hey I am trying to achieve the same long polling chat with brukva/pub-sub but its lack of documentation and I see you stuck in same place with passing callback objects in long polling, do you have any code examples about it ?

goldalworming commented 12 years ago

I was successfully make pub sub work as I wish...but unfortunately my code was cleaned up while I installing new linux... if you don't mind, let me see your code...maybe I could help..

Pada 10 Desember 2011 19:32, Burak Dede < reply@reply.github.com

menulis:

@goldalworming Hey I am trying to achieve the same long polling chat with brukva/pub-sub but its lack of documentation and I see you stuck in same place with passing callback objects in long polling, do you have any code examples about it ?


Reply to this email directly or view it on GitHub: https://github.com/evilkost/brukva/issues/13#issuecomment-3090615

burakdede commented 12 years ago

Here is part of my code. https://gist.github.com/1455409

"listing_id" will be the channel name that user subscribe but I dont know how to write "wait_for_messages" and "new_messages" in MesssageMixin class.

burakdede commented 12 years ago

@goldalworming any chance you can look at code sample ? sorry for the rush I need to have it done fast.

goldalworming commented 12 years ago

I'm sorry I was totally lost my code...but something I can tell you is I didn't really need messagemixin anymore while using burkva..

it was just asynchronously fetch the new messages in redis.

Pada 10 Desember 2011 23:09, Burak Dede < reply@reply.github.com

menulis:

@goldalworming any chance you can look at code sample ? sorry for the rush I need to have it done fast.


Reply to this email directly or view it on GitHub: https://github.com/evilkost/brukva/issues/13#issuecomment-3091503

burakdede commented 12 years ago

thats a good news using with redis MessageMixin seems just extra anyway. any change in javascript side of the chat.js ?

burakdede commented 12 years ago

is it appropriate to initiate connection to redis everytime like in the MessageUpdateHandler just like you did ? Seems like one connection fits fine...

goldalworming commented 12 years ago

chat.js update is depend on your need,

I little forgot about create connection to redis while message handler...but the clue is just follow the code like burkva example

Pada 10 Desember 2011 23:56, Burak Dede < reply@reply.github.com

menulis:

is it appropriate to initiate connection to redis everytime like in the MessageUpdateHandler just like you did ? Seems like one connection fits fine...


Reply to this email directly or view it on GitHub: https://github.com/evilkost/brukva/issues/13#issuecomment-3091701

goldalworming commented 12 years ago

@burakdd this is the code, I rewrite it for example

https://github.com/goldalworming/tornado-redis-chat

burakdede commented 12 years ago

@goldalworming thanks I checked your code, you have to call "unsubscribe" after calling finish cause you probably get error below cause its sendin empty post return for the first time and then callback executed and call finish again so better unsubscribe client there. I am also looking if it is right way to connect new client each time new post call cause when I check redis console its adding 7 client even 2 connected and sended 2 messages... But thanks for the code again.

"""RuntimeError: finish() called twice. May be caused by using async operations without the @asynchronous decorator. [E 111212 00:51:02 web:657] Cannot send error response after headers written"""

goldalworming commented 12 years ago

@burakdd I am very pleased to know how you solve it.