This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

MQTT Chat

This sample demonstrates how WSO2 MB can be used to create a chat client that uses MQTT.

About the sample

The <MB_HOME>/Samples/MqttChatClient/src/main/java/org/wso2/sample/mqtt directory has the following classes:

Prerequisites

Before you build the sample, the prerequisites for MB samples should be in place.

Building the sample

If you are building an MQTT sample for the first time, you need to build the sample using Maven. This will download the Maven dependencies needed for your MQTT samples. Once the dependencies are downloaded, you can build any of the MQTT samples using either Maven or Ant.

Using Maven:
  1. Navigate to the MqttChatClient sample folder in the <MB_HOME>/samples directory.
  2. Execute the mvn clean install command to build and run the sample.
  3. If the build is successful, you can analyze the output as shown below.
Using Ant:

Be sure that the Maven dependencies required for MQTT samples are already downloaded as explained here.

  1. Navigate to the MqttChatClient sample folder in the <MB_HOME>/samples directory.

  2. Execute the ant command to build and run the mqtt retain sample.

  3. If the build is successful, you can anlyze the output as shown below.

Analyzing the output

Once you run the sample, you can carry out the following activities.

ActivityCommand
Sending personal messages or group messages.name/group_name message to send
Joining a group chat join group_name
Leaving a group chatjoin group_name
Exiting the sampleexit

Example

  1. Run two instances of this sample by running the ant or the mvn clean install command from the <MB_HOME>/samples/MqttChatClient directory directory in two consoles. 
  2. You will be requested for a chat alias in both instances. Enter abc in one terminal and def in the other. The following would appear in the log of both instances.

    [java] >Use <alias/group message> to chat to a desired group or a person
    [java] <join group_name> to join a group chat
    [java] <leave group_name> to leave a group chat
    [java] <exit> to exit
  3. Enter the following command in the terminal of the abc chat alias.

    def Hello 

    The following will appear in the log of the def chat alias.

    [java] >Personal message from abc
    [java] hello
    [java] Waiting for your input. Use <help> for more info
  4. Connect both chat aliases to a group chat using the following command.

    join Chat1Group

    You will get the following log for both instances.

    [java] >Joined to the group : Chat1Group
  5. Enter the command leave Chat1Group in the terminal for ABC chat alias. The following would appear in the log.

    [java] >Left the group : Chat1Group
  6. Type exit in the same terminal to stop the chat application.