Creating a Private Discussion Group
A private discussion group allows you to create an "invite-only" discussion group so that people can discuss a topic privately.
Create Channel
Start by creating a channel object:
Max.Channel.create({
name: 'myPrivateChannel',
summary: 'my private channel description',
isPublic: false,
publishPermissions: 'subscribers'
}).success(function(myPrivateChannel) {
// success
}).error(function(err) {
// failure
});
Discover Private Channels
You can discover a private channel by name or view all your private channels. Note that only the channel owner discover a channel by name or view their list of private channels.
Invite Users to Channel
Invite users to join a channel:
var invitees = [user1, user2];
var comments = 'Subscribe to the Company Policies channel';
myPrivateChannel.inviteUsers(invitees, comments).success(function() {
// invitation has been sent
});
The EventListener is leveraged to listen for incoming invites:
var listener = new Max.EventListener('myListener', {
invite: function(myInvite) {
// invitation has been received
var comment = myInvite.comment;
var sender = myInvite.sender;
var channel = myInvite.channel;
}
});
Max.registerListener(listener);
Let's get more information about the channel, like name, summary and owner.
var myInvitedChannel = myInvite.channel;
var name = myInvitedChannel.name;
var summary = myInvitedChannel.summary;
var isPublic = myInvitedChannel.isPublic;
var ownerUserId = myInvitedChannel.ownerUserID;
var creationDate = myInvitedChannel.creationDate;
Accept Invite
Accept an invite.
var comments = 'sure, thank you!';
myInvite.accept(comments).success(function() {
// invitation has been accepted. current user is now subscribed to the channel
}).error(function() {
// handle error
});
Decline Invite
Decline an invite.
var comments = 'no thanks, maybe next time.';
myInvite.decline(comments).success(function() {
// invitation has been declined
}).error(function() {
// handle error
});
Receive Invite Responses
To receive an invitation response:
var listener = new Max.EventListener('myListener', {
inviteResponse: function(myInviteResponse) {
// invitation response has been received
var user = myInviteResponse.sender; // the invitation response sender
var channel = myInviteResponse.channel;
var comments = myInviteResponse.comments;
var hasAcceptedInvite = myInviteResponse.accepted; // true if invitee accepted
}
});
Add Subscribers
To add subscribers to your private channel, refer to the following code. Note that only the private channel owner can add subscribers.
var query = {userName: 'john.doe'};
var limit = 10;
var offset = 0;
Max.User.search(query, limit, offset).success(function(users) {
// add a subscriber to the channel
var johnDoeUser = users[0];
myPrivateChannel.addSubscribers(johnDoeUser).success(function() {
// success
}).error(function(err) {
// failure
});
});
Get User Subscription List
To get the list of channels that the current user is subscribed to (both private and public):
Max.Channel.getAllSubscriptions().success(function(subscribedChannels) {
// do something with the subscribed channels
}).error(function(err) {
// handle error on failure
})
Publish Message to Channel
To publish a message to the private channel:
var msg = new Max.Message({
myCustomKey: 'message to private channel'
});
myPrivateChannel.publish(msg).success(function() {
// message has been published!
}).error(function(err) {
// handle error on failure
});
Retrieve Messages
When logged in, messages can either be retrieved using myPrivateChannel.getMessages()
or can be received by setting up a listener with Max.EventListener
:
var listener = new Max.EventListener('myListener', {
message: function(myMessage) {
// message has been received
if (myMessage.channel.name == 'myPrivateChannel') {
// do something with received message
}
}
});
Max.registerListener(listener);
// remember to unregister the listener if no longer used to prevent leaks `Max.unregisterListener('myListener');`
Remove Channel Subscriber
To remove a subscriber from a private channel, refer to the following code. Note that only the private channel owner can remove subscribers.
var query = {userName: 'john.doe'};
var limit = 10;
var offset = 0;
Max.User.search(query, limit, offset).success(function(users) {
// add a subscriber to the channel
var johnDoeUser = users[0];
myPrivateChannel.removeSubscribers(johnDoeUser).success(function() {
// success
}).error(function(err) {
// failure
});
});
Useful Links