Creating a Public Forum

A public forum is a common way to share information with other users. Forums are often organized around a particular subject. The following use case illustrates how to create a public channel using our APIs.

Create Channel

In Magnet Message, our channel object is how messages are sent to and received by a group. To create a "SFgiants" channel:

Max.Channel.create({ name: 'SFGiants', summary: 'The Giants Channel' }).success(function(mmxPublicChannel) { // success }).error(function(err) { // failure });

NOTE: The channel creator is automatically subscribed to the channel.

Discover Channels

Now we want to discover channels that you can subscribe to.

  • To discover all channels that start with "SF" in their name:

    var prefix = 'SF'; var limit = 10; var offset = 0; Max.Channel.findPublicChannels(prefix, limit, offset).success(function(channels) { // success }).error(function(err) { // failure });

  • To discover all public channels, leave the channel prefix null:

    var limit = 10; var offset = 0; Max.Channel.findPublicChannels(null, limit, offset).success(function(channels) { // success }).error(function(err) { // failure });

  • To find a specific public channel:

    var channelName = 'SFGiants'; Max.Channel.getPublicChannel(channelName).success(function(channel) { // success }).error(function(err) { // failure });

Check Channel Subscription

After discovering a channel, check if the current user is subscribed to the channel.

var sfGiantsChannel = ...; if (sfGiantsChannel.isSubscribed) { // I'm following the SF Giants channel }

Subscribe to Channel

Let's subscribe to the "sfGiants" channel:

var sfGiantsChannel = ...; if (!sfGiantsChannel.isSubscribed) { sfGiantsChannel.subscribe().success(function() { // success }).error(function(err) { // failure }); }

Unsubscribe from Channel

To unsubscribe from the "sfSeals" channel:

var sfSealsChannel = ...; if (sfSealsChannel.isSubscribed) { sfSealsChannel.unsubscribe().success(function() { // success }).error(function(err) { // failure }); }

View Channel Subscribers

After subscribing, you can see other subscribers in the "sfGiants" channel:

var sfGiantsChannel = ...; var limit = 10; var offset = 0; sfGiantsChannel.getAllSubscribers(limit, offset).success(function(users) { // returns a list of subscribed users }).error(function(err) { // handle error on failure });

Get Published Messages on Channel

Let's get the "sfGiants" channel content for the last hour:

var sfGiantsChannel = ...; var startDate = new Date(); startDate = startDate.setHours(startDate.getHours() - 1); var endDate = new Date(); var limit = 10; var offset = 0; var ascending = true; sfGiantsChannel.getMessages(startDate, endDate, limit, offset, ascending).success(function(messages) { // do something with messages }).error(function(err) { // handle error on failure });

Publish Message to Channel

To publish a message to the "sfGiants" channel:

var sfGiantsChannel = ...; var channelMsg = new Max.Message({ myCustomKey: 'message to channel' }); sfGiantsChannel.publish(channelMsg).success(function(messageId) { // published message; messageId returned onsuccess }).error(function(err) { // handle error on failure });

Register for Message Notifications

When logged in, messages can either be retrieved using sfGiantsChannel.getMessages() or can be received by setting up a listener with Max.EventListener:

var listener = new Max.EventListener('myListener', { message: function(receivedMessage) { // message has been received if (receivedMessage.channel.name == 'SFGiants') { // do something with received message } }, invite: function(myInvite) { // invitation has been received }, inviteResponse: function(inviteResponse) { // invitation response has been received } }); Max.registerListener(listener); // remember to unregister the listener if no longer used to prevent leaks `Max.unregisterListener('myListener');`


Next Steps

See how to create a Private Discussion Group in your app.