leporo / tornado-redis

Asynchronous Redis client that works within Tornado IO loop.
667 stars 163 forks source link

Executing non pubsub command after unsubscribe #29

Closed quard8 closed 11 years ago

quard8 commented 11 years ago

I'm trying to implement simple long-polling chat and have strange situation. When adding new message, message id will be pushed to channel. On updates handler I subscribing to that channel. But I can't execute any commands, because I got exception

RequestError (on ZRANGEBYSCORE [('messages', '22', '22'), {}]): Calling not pub/sub command during subscribed state

I'm doing like this:

    @tornado.web.asynchronous
    @tornado.gen.engine
    def on_pubsub_message(self, message):
        if message.kind == 'message':
            yield tornado.gen.Task(self.client.unsubscribe, 'chat_messages')
            messages = yield tornado.gen.Task(self.client.zrangebyscore, 'messages', message.body, message.body)
            result = []
            for m in messages:
                result.append(json.loads(m[0]))

            response = {
                'm': result[::-1]
            }
            self.set_header('Content-Type', 'application/json')
            self.finish(json.dumps(response))

So you can see that I'm unsubscribing and still get error. What I'm missing?

leporo commented 11 years ago

Here are my suggestions:

quard8 commented 11 years ago

Thanks for quick answer! I will try to use global Client.

WebSocket's are great, but for iOS application it still painfull to check connections on app close.

Thanks, anyway!

leporo commented 11 years ago

Oh, I didn't mentioned in my previous answer that you may use long-polling connections with sockjs.

Please check the documentation and client source code here: https://github.com/sockjs/sockjs-client#sockjs-client-api. May be it would me easier to implement sockjs connection protocol in your iOS application and get the websocket connection support (to be used by Android or web applications) "for free"?

However, it's only a suggestion. Thank you.