logux/server

Multi-subscribe or Push subscribtions to client?

orlov-vo opened this issue · 2 comments

I trying to implement simple application with Logux. And I have some questions about implementing loading subresources. For example I have a simple blog with posts and related comments. On client I want to subscribe to post with id X, and to first 10 comments.

On server I created two channel: posts/:id and comments/:id.

On client I do this steps for loading:

  1. Send action { type: "logux/subscribe", channel: "posts/X" }
  2. Send action { type: "posts/get-comments", id: X, count: 10, from: 0 }
  3. After recive reply from second action I send 10 actions like this: { type: "logux/subscribe", channel: "comments/Y" }

For me 12 actions for it is overhead, isn't it?

ai commented

It is better to have channel posts/:post_id/comments and subscribe just to 2 channels.

You can use subscription options to make pagination:

// client
useSubscription([{ channel: 'posts/1/comments', count: 10, from: 0 }])

// server
server.channel('posts/:post/comments', {
  
  async init (ctx, action, meta) {
    action.count //=> 10
  }
  filter (ctx, action, meta) {
    return (action, meta) => {
      // filter action with meta.channels: ['posts/1/comments'] according action,count and action.from
    }
  }
})

Will it work for you?

Sounds good! I will try to use it