jrief / django-websocket-redis

Websockets for Django applications using Redis as message queue
http://django-websocket-redis.awesto.com/
MIT License
896 stars 222 forks source link

Understanding the fetch_message() #117

Closed Seraf closed 9 years ago

Seraf commented 9 years ago

Hello,

First, thanks for your work, I've implemented it on my project in 5 minutes and it worked like a charm. My application is a web api done with django-rest-framework that launch a celery task. The api return a taskid (which is the facility used by ws4redis).

I can connect on ws://localhost:8000/ws/61a1d3f8-5f7e-489f-9aef-bb5d5dbe64f5?subscribe-broadcast and I receive the flow, it's awesome everything works. I just have the need to display all the messages already sent before the first connection.

I saw https://github.com/jrief/django-websocket-redis/blob/master/ws4redis/publisher.py#L19 which seems to have been done for that, but don't understand how to use it in my case. I don't want to create a view templated, just expose a ws flow.

Thanks for your help

jrief commented 9 years ago

I just have the need to display all the messages already sent before the first connection.

maybe I do not understand this correctly.

You have one message per facility and audience. Its stored inside a Redis bucket. If you need more than one message, store a JSON object inside Redis and deliver that.

Seraf commented 9 years ago

Hello, thanks for your answer. My usecase is : I launch a task from the api. This task will publish a message for each log entry it will generate. My api return the websocket url. I don't know when the client will open the websocket connection. I want to return him all the entries and not only the new ones published since his connection (my actual behavior).

Le ven. 30 oct. 2015 12:26, Jacob Rief notifications@github.com a écrit :

I just have the need to display all the messages already sent before the first connection.

maybe I do not understand this correctly.

You have one message per facility and audience. Its stored inside a Redis bucket. If you need more than one message, store a JSON object inside Redis and deliver that.

— Reply to this email directly or view it on GitHub https://github.com/jrief/django-websocket-redis/issues/117#issuecomment-152499993 .

jrief commented 9 years ago

Do the following: Whenever the client accesses to site for the first time, load everything he requires using plain JSON objects in HTML, or Ajax.

Now, whenever there is an update and your task has to push additional data to the client, push that data though Redis to the client. There it can be merged with his existing JS object.

Seraf commented 9 years ago

Seems a bit overkill to me. There's something I don't understand : as Redis act as a queue, it seems logic for me that a message "not consumed" can be retrieved by a new connection as we set an expire.

There's probably something I don't get. I'm looking on the code to understand it better.

Seraf commented 9 years ago

Ok, I got it, there's only one key that is overwritten each time a new message is published under the same facility. I better understand it now :)

So, do you think it can be interesting in my case to overwrite the subscriber and store all messages published in another redis key and when a new connection open, get all serialized messages from this key and deliver them (https://github.com/jrief/django-websocket-redis/blob/master/ws4redis/subscriber.py#L51) then do the traditionnal pub/sub ?

jrief commented 9 years ago

In Redis the messagequeue and the data buckets are independent. For ws4redis makes it a very interesting Django app.