|
// model is defined using the browserified client |
|
var ChatRoom = app.models.ChatRoom; |
|
var User = app.models.User; |
|
|
|
function login(username, password, cb) { |
|
User.login( |
|
{ username: username, password: password}, |
|
function(err, token) { |
|
if (err) return cb(err); |
|
ChatRoom.find(function(err, allRooms) { |
|
if (err) return cb(err); |
|
|
|
// TODO: GUI - render list of rooms |
|
|
|
ChatRoom.on('changed', function(room) { |
|
// TODO: GUI - add a new room to the list |
|
}); |
|
|
|
ChatRoom.on('deleted', function(roomId) { |
|
// TODO: GUI - remove the room from a list |
|
}); |
|
cb(); |
|
}); |
|
}); |
|
} |
|
|
|
function join(room, cb) { |
|
room.join(function(err) { |
|
if (err) return cb(err); |
|
|
|
async.parallel([ |
|
function fetchPosts(next) { |
|
room.posts(function(err, posts) { |
|
if (err) return next(err); |
|
|
|
// TODO: GUI - render a list of existing posts |
|
|
|
room.on('posted', function(post) { |
|
// TODO: GUI - add the new post to the chat window |
|
}); |
|
|
|
next(); |
|
}); |
|
}, |
|
|
|
function fetchUsers(next) { |
|
room.users(function(err, users) { |
|
if (err) return next(err); |
|
|
|
// TODO: GUI - render a list of users |
|
|
|
room.on('joined', function(user) { |
|
// TODO: add user to the list of connected users |
|
}); |
|
|
|
next(); |
|
}); |
|
}, |
|
], cb); |
|
}; |
|
} |
|
|
|
function post(room, message, cb) { |
|
room.post(message, function(err, msg) { |
|
if (err) return cb(err); |
|
// TODO: GUI - add the new post to the chat window |
|
cb(); |
|
}); |
|
} |
Oh cool... so this is using the original remote
on()
method approach. I like how easy this makes creating rooms... Under the hood I'm not sure how we can ensure only events emitted on a model instance will be sent to users who are listening. My current thinking is:The other benefit of using
on()
as a remote method is that you can protect data sent over events by controlling access (ACLs) to theon()
method (which could be a READ operation).Here's what an ACL might look like to secure the data sent over web sockets: