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:
- Navigate to the MqttChatClient sample folder in the
<MB_HOME>/samples
directory. - Execute the
mvn clean install
command to build and run the sample. - 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.
Navigate to the MqttChatClient sample folder in the
<MB_HOME>/samples
directory.Execute the
ant
command to build and run the mqtt retain sample.- 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.
Activity | Command |
---|---|
Sending personal messages or group messages. | name/group_name message to send |
Joining a group chat |
join group_name
|
Leaving a group chat | join group_name |
Exiting the sample | exit
|
Example
- Run two instances of this sample by running the
ant
or themvn clean install
command from the<MB_HOME>/samples/MqttChatClient
directory directory in two consoles. You will be requested for a chat alias in both instances. Enter
abc
in one terminal anddef
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
Enter the following command in the terminal of the
abc
chat alias.
def Hello
The following will appear in the log of thedef
chat alias.[java] >Personal message from abc [java] hello [java] Waiting for your input. Use <help> for more info
- 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
Enter the command
leave Chat1Group
in the terminal forABC
chat alias. The following would appear in the log.[java] >Left the group : Chat1Group
Type
exit
in the same terminal to stop the chat application.