Meteor.js: Deps.autorun and subscriptions

1,098 views
Skip to first unread message

Matt Darby

unread,
Nov 13, 2013, 2:16:17 PM11/13/13
to meteo...@googlegroups.com

I've recently picked up MeteorJs and really dig it. I've gotten 99% of my app to work, except for this one nagging bug (I've been chasing it for two days now).

The issue is logging in, or more appropriately, what happens after a user signs in, or when a user creates an account. My code below works, but it appears to be a timing issue of sorts.

If I take out the containing call to Deps.autorun, sign in fails as it doesn't find a room. BUT, if then creating an account fails as it doesn't have a user profile. Note that even when it fails, I can refresh the page and everything works. I've stepped through the code a hundred times and this has to be it.

Anything glaringly obvious?

Deps.autorun ->
  if Meteor.user()
    user   = Meteor.user()
    userId = Meteor.userId()
    email  = user.emails[0].address

    Meteor.subscribe "rooms", email

    if user.profile is undefined
      if Rooms.findOne() is undefined
        roomId = Rooms.insert({name: "Watercooler", ownerId: userId, inviteeEmails: [], roster: []})
      else
        roomId = Rooms.findOne()._id

      Meteor.users.update({_id: userId}, {$set:{'profile.lastRoomId': roomId, 'profile.showGravatars': true}})
    else
      roomId = user.profile.lastRoomId

    Meteor.subscribe "connections"
    Meteor.subscribe "messages", roomId
    Meteor.subscribe "last_in_room", roomId
    Meteor.call 'addUserToRoster', roomId

    Meteor.setInterval ->
      Meteor.call 'keepalive', userId
    , 5000

Abigail Watson

unread,
Nov 13, 2013, 2:47:07 PM11/13/13
to meteo...@googlegroups.com
There's lots of subtle things happening here involving database flapping, the event cycle, the client/server login process, etc. that I'm not sure it's entirely worth getting into.  Suffice it to say that you're not the only one who has experienced this, and the short answer is to check out the event-hooks package in Atmosphere.

The longer answer involves:
- Using Meteor.autorun() instead of Deps.autorun() 
- Don't try to do the Rooms.insert() during subscription; database flapping will cause that to bork.  
- Instead, create a template level helper to insert a room object if the returned collection is null or undefined 
- Similarly, your Meteor.users.update won't work if the database is flapping and user.profile is undefined
- Try adjusting things to more of the following pattern, which handles the database flapping a bit more explicitly:

if(Meteor.user()){
  if(Meteor.user.profile){
    if(Meteor.user.profile.lastRoomId){
      //do stuff
    }
  }
}

Also, instead of rolling your own keepalive, you might like the user-status package.

Matt Darby

unread,
Nov 13, 2013, 3:57:24 PM11/13/13
to meteo...@googlegroups.com, Abigail Watson
Thank you very much Abigail. I’ll work towards those patterns and report back. :)

Cheers!

Matt Darby, M.S.
Ruby | Rails | iOS

Skype: matt-darby
--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/8lKB47kSSgY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

James Gill

unread,
Nov 16, 2013, 8:37:00 PM11/16/13
to meteo...@googlegroups.com


On Wednesday, November 13, 2013 6:47:07 AM UTC-8, Abigail Watson wrote:\
The longer answer involves:
- Using Meteor.autorun() instead of Deps.autorun() 

I don't understand this suggestion. Meteor.autorun() is deprecated in favor of Deps.autorun(), and it is now just an alias for backwards compatibility.  Or is there something I'm missing?

James 

David Glasser

unread,
Nov 16, 2013, 8:42:19 PM11/16/13
to meteo...@googlegroups.com
James, you are correct.


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages