I am running into a conceptual problem with SG and channels and am wondering if it is a limitation of the model or if I am just not thinking about things correctly. I am relatively new to SG (just migrating from CouchDB) but I have found SG channels to be quite flexible – every-time I’d think it wouldn’t handle a case I would figure out a solution, but one isn’t coming quickly this time.
Imagine a large-scale IRC-style chat app (this is not quite what I am building, but the same problem and easier to understand)
Room Documents - (type=room) - let’s say 100K of them
- room title, description, logo, synopsis, rules, etc
- some rooms are public, some are private / members-only
- published on room/(room-id) channel
User Documents (type=user) - let’s say 100K of them
- user information, avatars, etc.
- published on user/(user-id) channel
“Memberships” (type=membership) - let’s say 1 million of them
- relates users to rooms they are interested in or have joined
- holds room-id, user-id, join date, membership status, etc
- sync function uses this to grant user-id access to room/(room-id) channel
- some users have only joined 1 rooms, some have 1000 rooms
- published on user_memberships/(user-id) channel
- published on room_memberships/(room-id) channel
At startup, the client app needs to quickly sync documents for all the rooms joined by the user, as well as their membership documents. The client doesn’t want to sync all the rooms available, since that would be huge and slow.
Ideally, I’d like to generate the channel rooms/(user-id) that contains the room documents for that user. This would allow syncing this channel first, and then making the app usable while doing sync of other data. I can’t figure out how to generate this rooms/(user-id) channel – Am I correct that this is just not possible?
The workarounds I am contemplating:
- Sync the user_memberships(this-user-id) first, and then build a list of room channels for rooms found in the membership documents and sync those channels. – OR –
- Enumerate the channels the user has access to via the SG REST user API and filter the channels strings to find room/* and user_memberships/*, and sync those resulting channels.
Once that is done I can continue syncing other channels I find interesting in the background. This startup solution isn’t ideal – so I’m wondering if there is possibly a way to generate the rooms/(user-id) channel that I’m missing.
I looked at using roles for this, but since the list of room names changes dynamically, and is potentially a large number, and roles have to be statically defined, I don’t think that works.
As a followup, what if I wanted to sync the list of users for each of the rooms that I subscribe to, so I have the full profiles of all other users that are members in the rooms in which I am a member? I can’t figure out that one either. It would be nice to create room_users/(room-id) channel but that doesn’t seem possible since users are related to rooms by memberships.
Am I thinking about this the right way? Is this truly just a limitation of the channel model? If so I’ll do the workaround on the first one, and could try a similar workaround for the second case, but it would involve syncing lots of individual channels using cumbersome algorithms, and I would rather know I’m not overlooking something to those who are more experienced in using channels.