Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduled messages sent by server #245

Closed
albertored opened this issue Jul 14, 2016 · 6 comments
Closed

Scheduled messages sent by server #245

albertored opened this issue Jul 14, 2016 · 6 comments

Comments

@albertored
Copy link

I need to create a webpage that shows the status of some application on server side.

For doing this I want clients to connect to server via websockets and the server pushing messages every n seconds on that websockets. This can be easily done using celery scheduled tasks. In particular I have a code similar to this

@periodic_task(run_every=timedelta(seconds=5))
def send_status():
    Channel("status").send()

where the channel is associated with a consumer that collects the information from the system and sends them to the Group

def ws_status(message):
    # collecting info...
    Group("status").send(collected_info)

This works pretty good. My problem is that collecting the status can be a little bit demanding and I don't want that piece of code being executed when no client is actually receiving those information. (It will happen often since it is a webpage built for being used by few users).

At the moment I solved this by setting a boolean variable and putting a check on it onto the ws_status consumer: if False information are not collected, if True they are collected, sent to the group and the variable is set to False. Then, at the client side, I have a script that sends an acknowledgement on receiving a new status, and a corresponding consumer on the server that set again the variable to True on receiving acknowledgment.

But I don't think this is a good solution, at the moment it works but maybe there are some caveats that I have not yet found.

@michaelkuty
Copy link
Contributor

From my point of view is this issue similar to #74

@albertored
Copy link
Author

Yes, it is surely related to that issue but not equal.

Here the problem is not how to run scheduled jobs but how to interrupt scheduled jobs if no one is listening. Both using celery or cron the problem remains, there are situations in which it is useful to have a way of knowing if the Group is empty

@michaelkuty
Copy link
Contributor

michaelkuty commented Jul 14, 2016

Yes i solving this using cache, i save all users and count to the cache because for me is not critical to have this persistent, you could easily handle ws connect and save all information which you needs, but maybe would be good to have native way for doing this

@andrewgodwin what you thinking about storing this information in channel backend ?

I think that we could propose some preferred solutions for doing this

@andrewgodwin
Copy link
Member

I would say this is actually #174 in that case (have a way to see if a group is empty or not), which I am still not totally okay making the channel backend deliver as a status, because it tightens the contract a little more.

Is there a reason you don't just want to leave it sending into the void? A Group.send() with no listening channels is basically free.

@michaelkuty
Copy link
Contributor

I write simple code for handling group related data which now uses cache but is backend independent

from .base import BaseManager

users = BaseManager('mygroup', 'users')

# on connect
users.add_item(user)

# on disconnect
users.delete_item(user.id)

users.get(user.id)
users.count()
users.all()


class AdminsManager(BaseManager):

    scope = 'admins'


admins = AdminsManager('mygroup')

admins.add_item(user_admin)

admins.all()

as you mention its extra calls on client connect

code lives here https://github.com/leonardo-modules/leonardo-channels/blob/feature/ws_frontend/leonardo_channels/managers/base.py

@andrewgodwin
Copy link
Member

Feature requested for this was done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants