Django channel 2 - Provide seperate feeds to each users every 5 second

77 views
Skip to first unread message

sahadev sahu

unread,
May 19, 2018, 5:10:30 AM5/19/18
to Django users
Hello,

I am implementing a websocket application for live data feed updates to each client according to their watchlist in every 5 second. Here I am explaining my requirement:

  1. Once user register with my application, he can view list of currency pairs e.g (ETH, BTC etc)
  2. From that list, user can add one or more pairs into his watchlist.
  3. And based on user's watchlist, user should get only that data through "Channel" which is in his watchlist in every 5 second

Possible approach as per me:

  1. Should i create multiple channel for each user ? or
  2. Is there possibility to create multiple events in single channel to handle each user separately to send different feeds to each user

Note: Every feed will fetch its results from another exchange website, then the end user will get that feed into their watchlist. How can i run my script continuously for each user seprately


Please kindly suggest, what is the right approach to implement this kind of situation.

Ryan Nowakowski

unread,
May 20, 2018, 3:43:42 PM5/20/18
to django...@googlegroups.com
On Fri, May 18, 2018 at 10:28:00PM -0700, sahadev sahu wrote:
> Hello,
>
> I am implementing a websocket application for live data feed updates to
> each client according to their watchlist in every 5 second. Here I am
> explaining my requirement:
>
> 1. Once user register with my application, he can view list of currency
> pairs e.g (ETH, BTC etc)
> 2. From that list, user can add one or more pairs into his watchlist.
> 3. And based on user's watchlist, user should get only that data through
> "Channel" which is in his watchlist in every 5 second
>
> Possible approach as per me:
>
> 1. Should i create multiple channel for each user ? or
> 2. Is there possibility to create multiple events in single channel to
> handle each user separately to send different feeds to each user
>
> Note: Every feed will fetch its results from another exchange website, then
> the end user will get that feed into their watchlist. How can i run my
> script continuously for each user seprately
>
>
> Please kindly suggest, what is the right approach to implement this kind of
> situation.

Here's one way to do it. Each channel should be associated with
a group[1]. There should be one group per User[2] associated via a
database model[3](called UserWebsocketGroup). Then you can poll the
exchange website using a background task[4] that goes something like this:

def poll_exchange_site():
user_websocket_groups = UserWebsocketGroup.objects.all()
for user_websocket_group in user_websocket_groups:
currency_pairs = user_websocket_group.user.currency_pairs.all()
for pair in currency_pairs:
pair_values = get_pair_values_from_exchange(pair)
user_websocket_group.group.send({pair: pair_values})
time.sleep(5)
poll_exchange_website()


[1] http://channels.readthedocs.io/en/latest/topics/channel_layers.html#groups
[2] https://docs.djangoproject.com/en/2.0/ref/contrib/auth/#user-model
[3] https://docs.djangoproject.com/en/2.0/topics/db/models/
[4] http://channels.readthedocs.io/en/latest/topics/worker.html#
Reply all
Reply to author
Forward
0 new messages