deepstream.io icon indicating copy to clipboard operation
deepstream.io copied to clipboard

list getEntries limit and default sort

Open reactor81 opened this issue 9 years ago • 9 comments

simple chat app..

I want a list of records to be my message names/keys. I want to get the list when user loads the app, but only the last 50 names/keys.. I have data already with 2 000 000 messages. Do I have to load all 2 million keys and then subscribe to 50 records? Even though its just keys, sending 2M keys over the wire seems brutal if I only need 50.

And then, depending on if there is a way to only select 50, what order will they be selected out of the list?

Thank you

reactor81 avatar Jul 01 '17 12:07 reactor81

the best way to implement this is to have a backend provider that provides a dynamic list, or a much easier way would be to make an RPC initially to get the last 50, and then subscribe to an event for each new chat line which gets added into the list.

We are looking to add pagination into lists soon to solve this issue, but one of the issues is it makes the implementation quite specific to each database/cache and we need to find a decent abstraction layer around it first.

I hope that helps. If you can give me some more details on your type of cache and database I can go a little more in depth

yasserf avatar Jul 01 '17 20:07 yasserf

Hmmm. I am pairing deepstream with MongoDB but no cache yet, will add Redis Cache later as needed.

So, are you recommending I just grab 50 records out of mongodb directly and subscribe to those 50 chat messages?

To be honest, I don't really need to subscribe to the chat messages, they don't change (no edit/delete). It's really the chatroom that I need instant sync for - the list of messages.

So the recommendation is to pull the initial chat messages (including message content) from MongoDB via mongoose/graphql first.. And then only use deepstream as a Pub/Sub for future messages? I can do that.. But then deepstream is just acting as a pubsub channel for me, hmmm.

reactor81 avatar Jul 01 '17 22:07 reactor81

The main issue is that lists are currently loaded into the client/server and if you have two million messages would be quite intense. The benefit of using deepstream is still quite a bit, you can apply permissions to who can actually emit events, authenticate users, set the data on the frontend and have it automatically save on the backend.

Until we have an aspect of windowing for lists you will need to either have a dynamic list generated in the backend (using something like https://www.npmjs.com/package/deepstream.io-provider-search-, rethinkdb) which can automatically generate realtime lists of limited sizes based on a table and any filter, or a combination as mentioned previously.

Crud pagination is super simple, but the issue is we want to have it in realtime which limits us to a couple of databases, a move we won't/can't make. However this issue is being discussed often internally and we'll hopefully have a solution that integrates collections and pagination in the future.

yasserf avatar Jul 02 '17 08:07 yasserf

@yasserf I see.. Thank you..

It sounds like I should maybe refactor and just use basic pub/sub on a channel to send events - and ignore the data structure part then.

The idea is a multi-chatroom type service, like slack / discourse. But many of the rooms have hundreds of thousands of messages. When user loads the page, I just want to load the first 50 messages or so, and render them in the react view. Then, I want to get notified of any new messages sent to that channel / room. It seemed like Firebase did a good job of this because they has startAt and limit functionality, but their bandwidth costs are high so I wanted to run my own.

Perhaps I should just do a simple socket.io or other channel-based pub/sub type service locally. Hmm.

Thanks again,

reactor81 avatar Jul 02 '17 09:07 reactor81

Also, the actual emitting event is only done on the server side, clients are 100% read only. So that simplifies my permissions quite a bit.

reactor81 avatar Jul 02 '17 09:07 reactor81

Clients send a relay/graphql mutation to the server, auth is done there in javascript/express and then events are emitted by deepstream. Although maybe I should replace the deepstream part with a dead-simple pub/sub.. Maybe Redis w/ socket.io is the way to go here?

reactor81 avatar Jul 02 '17 09:07 reactor81

So in deepstream the last thing you mentioned is:

Setting:

const room = 'xxx'
const data = { uuid, message: 'xxx' }
ds.record.setData(`${room}/${uuid}`, data) // this saves it into storage for you automatically
ds.event.emit(room, data) // this emits the event for all the subscribe clients

Subscribing:

function onMessage (message) {
  // process
}
ds.events.subscribe(room, onMessage)
ds.rpc.make('messages', { room: 'xxx', limit: 50 }, (err, messages) => messages.forEach(onMessage)

Backend:

ds.rpc.provide('message', (query, response) => {
  // db query  with query.filter and query.name
  response.success(messages)
})

So it's not exactly alot of stuff to do either, you can permission everything and it automatically takes care of writing stuff to the database for you. Keep in mind this is one limitation as well, but it hides away you having to run a javascript/express server and a pub/sub mechanism and interacting with your DB and Cache and applying permissions.

yasserf avatar Jul 02 '17 09:07 yasserf

We use this pattern alot for things like charting and historical data, where you get the last X amount of things from a database and then just listen to ticks as they happen. The only disadvantage is that you have to run a small service in the backend for the RPC, but its still alot less than a multitude of services.

yasserf avatar Jul 02 '17 09:07 yasserf

Hope that helped!

yasserf avatar Aug 10 '17 08:08 yasserf