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 public channel (SFGiants):

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

MMXChannel.create("SFGiants","The Giants Channel",true,new MMXChannel.OnFinishedListener<MMXChannel>(){ @Override public void onSuccess(MMXChannel mmxChannel) { //Success } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //Failure } });

Discover Channels

Next, discover channels that you can subscribe to:

  • To disover channels that start with SF:

int limit = 10; int offset = 0; String prefix = "SF"; MMXChannel.findPublicChannelsByName("SF", limit, offset, new MMXChannel.OnFinishedListener<ListResult<MMXChannel>>() { @Override public void onSuccess(ListResult<MMXChannel> mmxChannelListResult) { //Success } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //Failure } });

  • To discover all public channels:

int limit = 10; int offset = 0; MMXChannel.getAllPublicChannels(limit, offset, new MMXChannel.OnFinishedListener<ListResult<MMXChannel>>() { @Override public void onSuccess(ListResult<MMXChannel> mmxChannelListResult) { //Success } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //Failure } });

  • To get a specific public channel with a known name (SFGiants):

MMXChannel.getPublicChannel("SFGiants", new MMXChannel.OnFinishedListener<MMXChannel>() { @Override public void onSuccess(MMXChannel mmxChannel) { //Success } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //Failure } });

Check Channel Subscription

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

MMXChannel sfGiantsChannel = ...; if (sfGiantsChannel.isSubscribed()) { Log.d("follow", "I'm following the Giants!"); }

Subscribe to Channel

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

MMXChannel sfGiantsChannel = ...; // If I'm not following the Giants already, follow them now. if (!sfGiantsChannel.isSubscribed()) { sfGiantsChannel.subscribe(new MMXChannel.OnFinishedListener<String>() { public void onSuccess(String s) { ... } public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { ... } }); }

Unsubscribe from Channel

To unsubscribe from a channel (sfSeals):

MMXChannel sfSealsChannel = ...; // If I'm following the Seals already, unfollow them now. if (sfSealsChannel.isSubscribed()) { sfSealsChannel.unsubscribe(new MMXChannel.OnFinishedListener<Boolean>() { public void onSuccess(Boolean aBoolean) { ... } public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { ... } }); }

View Channel Subscribers

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

MMXChannel sfGiantsChannel = ...; int limit = 10; int offset = 0; sfGiantsChannel.getAllSubscribers(limit, offset, new MMXChannel.OnFinishedListener<ListResult<User>>() { @Override public void onSuccess(ListResult<User> userListResult) { //Success } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //Failure } });

Get Published Messages on Channel

To get all the channel content from the last hour:

MMXChannel sfGiantsChannel = ...; int limit = 10; int offset = 0; Date now = new Date(); Date anHourAgo = new Date(now.getTime() - (60 * 60 * 1000l)); boolean ascending = false; sfGiantsChannel.getMessages(anHourAgo, now, limit, offset, ascending, new MMXChannel.OnFinishedListener<ListResult<MMXMessage>>(){ @Override public void onSuccess(ListResult<MMXMessage> messages) { } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { } });

Publish Message to Channel

To publish a message to the channel:

MMXChannel sfGiantsChannel = ...; HashMap<String, String> content = new HashMap<String,String>(); String messageId = sfGiantsChannel.publish(content, new MMXChannel.OnFinishedListener<String>() { public void onSuccess(String s) { //successfully published a message to this channel } public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { //failed publishing to this channel } });

Get Channel Details

After discovering a channel or channels, you can get various channel details, including the latest messages and subscribers in one API call:

List<MMXChannel> channels = ...; MMXChannel.getChannelDetail(channels, new ChannelDetailOptions.Builder() .numOfMessages(10) // number of latest messages to retrieve .numOfSubcribers(5) // number of subscribers to retrive .build(), new MMXChannel.OnFinishedListener<List<ChannelDetail>>() { @Override public void onSuccess(List<ChannelDetail> channelDetails) { if (null != channelDetails && !channelDetails.isEmpty()) { } } @Override public void onFailure(MMXChannel.FailureCode failureCode, Throwable throwable) { } });

Retrieve Messages

When logged in, messages can either be retrieved using MMXChannel.getMessages() or by implementing MMX.EventListener.onMessageReceived():

MMXChannel sfGiantsChannel = ...; MMX.EventListener eventListener = new MMX.EventListener() { public boolean onMessageReceived(MMXMessage mmxMessage) { if (mmxMessage.getChannel().getName().equalsIgnoreCase(sfGiantsChannel.getName())) { //this is a message published to sfGiantsChannel ... } return true; //true consumes this message and prevents other listeners from getting called } }; MMX.registerListener(eventListener);


Next Steps

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