Question

I have an application where I want to use node.js and now.js to share the state of an object within a nowjs "group" across any number of clients.

As an example of what I'm trying to do, let's say that within the multiroom chat example that comes with now.js, there could be any number of rooms, with the ID chosen by the user. If the user knows the ID, they can enter (or be the first to "create") that room. There is a "history" object that is maintained for each room once it is created and communication starts happening. The history object contains a property or two, and an array of comment objects, each with a user property, a comment property, and a datetime property. When a new user connects, they should immediately have access to the history.

Is there a way to store a group-scoped object variable on the server to sync with? Am I thinking about this in the wrong way? Am I crazy or a little slow?

Was it helpful?

Solution

OK...I got it.

I have a new function createHistory(groupid) on the server side, which returns a new history object if it doesn't already exist when the user enters the room(group). This would occur if this user just created the room.

This history object employs add, remove and list methods. Since I need to get at the list from the client, it's important that I use a callback like the following:

api.get = (callback) ->
  callback(_history)

I assign the result:

everyone.getGroup(this.now.groupid).now.history = createHistory(this.now.groupid)

Now I can add history at the server:

api.add = (time,user,text) ->
  _history.items.push {time:time, user:user,text:text}

And get to it from my client:

now.history.get (history) ->
  for item in history
    #do something...

The only drawback is once the room is empty, the history evaporates...I think. Although I suppose you could check for the last disconnect and persist it somewhere if you wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top