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 describes how to create public forum functionality in your app and illustrates the use of the APIs by creating a channel for the SF Giants.

Create Channel

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

MMXChannel.createWithName( "sf_giants", summary: "Get the inside scoop on the San Francisco Giants", isPublic: true, publishPermissions: .Subscribers, success: { channel in // do something }, failure: { error in // do something })

Discover Channels

Next, discover channels that you can subscribe to.

  • To discover all public channels:

    MMXChannel.allPublicChannelsWithLimit(10, offset: 0, success: { totalCount, channels in // do something }, failure: { error in // do something })

  • To discover channels that include sf in their name:

    MMXChannel.channelsStartingWith("sf", limit: 10, offset:0, success: { totalCount, channels in // do something }, failure: { error in // do something })

Check Channel Subscription

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

let sfGiantsChannel: MMXChannel = ... if sfGiantsChannel.isSubscribed { print("I'm following the Giants!") }

Subscribe to Channel

To subscribe to a channel (sfGiants) so the user can send and receive messages:

let sfGiantsChannel: MMXChannel = ... sfGiantsChannel.subscribeWithSuccess({ // do something }, failure: { error in // do something })

Unsubscribe from Channel

To unsubscribe from a channel (sfSeals):

let sfSealsChannel: MMXChannel = ... sfSealsChannel.unSubscribeWithSuccess({ // do something }) { error in // do something }

View Channel Subscribers

After subscribing, you can see other subscribers to the channel (sfGiants):

let sfGiantsChannel: MMXChannel = ... myChannel.subscribersWithLimit(100, offset: 0, success: { count, subscribers in // do something }, failure: { error in // do something })

Get Published Messages on Channel

You can also get all messages published on the channel:

let sfGiantsChannel: MMXChannel = ... let dateComponents = NSDateComponents() dateComponents.hour = -1 let theCalendar = NSCalendar.currentCalendar() let now = NSDate() let anHourAgo = theCalendar.dateByAddingComponents( dateComponents, toDate: now, options: NSCalendarOptions(rawValue: 0)) sfGiantsChannel.messagesBetweenStartDate(anHourAgo, endDate: now, limit: 10, offset: 0, ascending: false, success: { totalCount, messages in // do something }, failure: { error in // do something })

Publish Message to Channel

To publish a message to the channel:

let messageContent = ["message":"Hello channel!"] myChannel.publish(messageContent, success: { message in print("Successfully published to \(myChannel)") }, failure: { error in print("Could publish to \(myChannel)\nError = \(error)") })

Register for Message Notifications

Register for notifications to receive messages that are published on the channel:

override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) // Indicate that you are ready to receive messages now! MMX.start() NSNotificationCenter.defaultCenter().addObserver( self, selector: "didReceiveMessage:", name: MMXDidReceiveMessageNotification, object: nil) }

Once incoming message notification is enabled, you can continue to implement your notification code as shown below:

func didReceiveMessage(notification: NSNotification) { let userInfo : [NSObject : AnyObject] = notification.userInfo! let message = userInfo[MMXMessageKey] as! MMXMessage } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) NSNotificationCenter.defaultCenter().removeObserver(self) }

Rename Channel

Additionally, a channel can be renamed. To rename a channel, call the rename method on the channel object.

let channel = ... channel.rename("new_name", success: { channel in //Channel renaming succeeded! }, failure: { error in //Channel renaming failed })

NOTE: Passing in a null name to the channel causes it to be automatically named with this format: “.NO_NAME.SDK_GENERATED_UUID”

Next Steps

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