0

So I am attempting to build a one on one chat app with firebase using marionette and backbone. I'm also using the Firebase Backbone bindings. I want there to be many users, each user has many conversation, and each conversation has many messages. Here's how my data looks:

users: {
  'user-id-1': {
    email: 'test@whatever.com',
    conversations: {
      'conversation-id-1': true,
      'conversation-id-2': true
    }
  },

  'user-id-2': {
    email: 'another@email.com',
    conversations: {
      'conversation-id-1': true,
    }
  },

  'user-id-3': {
    email: 'a-third@email.com',
    conversations: {
      'conversation-id-2': true,
    }
  }
}

conversations: {
  'conversation-id-1': {
    sender: 'user-id-1',
    receiver: 'user-id-2',
    messages: {
      'message-id-1': true,
      'message-id-2': true
    }
  },

  'conversation-id-2': {
    sender: 'user-id-1',
    receiver: 'user-id-3',
    messages: {
      'message-id-3': true,
      'message-id-4': true,
      'message-id-5': true,
    }
  }
}

messages: {
  'message-id-1': {
    sender: 'user-id-1'
    body: 'A message'
  },

  'message-id-2': {
    sender: 'user-id-2'
    body: 'A response'
  },

  'message-id-3': {
    sender: 'user-id-1'
    body: 'An inital message'
  },

  'message-id-4': {
    sender: 'user-id-3'
    body: 'Another response'
  },

  'message-id-5': {
    sender: 'user-id-1'
    body: 'Goodbye'
  },
}

What you can see from this:

User-1 has two different conversations going, each with User-2 and User-3

On a really basic level I am able to fetch all the conversations from the user with a function like so:

class Entities.User extends Backbone.Firebase.Model
  urlRoot: 'https://my-app.firebaseio.com/users'

  conversations: ->
    conversations = new Firebase('https://my-app.firebaseio.com/conversations')
    @firebase.child('conversations').on 'child_added', (snap) =>
      console.log snap.key()
      conversations(snap.key()).once 'value', (value) =>
        console.log value.val()
    undefined

This will log all of the conversations out when they are retrieved by firebase.

The problems are:

a) There's no real way for me to use a Backbone.Collection here to fetch just this users conversations, let alone the messages for that conversation, is there? I need a collection to pass to Marionettes CollectionView b) This just seems really messy in general.

Should I be structuring my data differently? I realize there's improvements that could be made to at least the way I am collecting the conversations so I could get an array.

What am I doing wrong? Any advice would be much appreciated.

Kato
  • 40,352
  • 6
  • 119
  • 149
goddamnyouryan
  • 6,854
  • 15
  • 56
  • 105
  • About structuring your data differently -- Check out this blog post: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html – sbolel Mar 02 '15 at 20:58
  • And this follow-up SO question http://stackoverflow.com/questions/21397425/clarification-on-firebase-denormalization-blog-post – sbolel Mar 02 '15 at 21:03
  • @SinanBolel i believe I structured my data correctly (I got my schema from that post). It's more about how to use this with backbone. – goddamnyouryan Mar 02 '15 at 21:11
  • 1
    Yep, was just suggesting some reading material. Something else that might be worth checking out is `firebase-util`, which you can use for joins. https://github.com/firebase/firebase-util#firebase-util – sbolel Mar 03 '15 at 00:40
  • 1
    @SinanBolel thanks! I did indeed find that already too, and it's super cool. I still find it hard to believe that no one has ever done this with backbone though! – goddamnyouryan Mar 03 '15 at 17:37

0 Answers0