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>/samplesdirectory.Execute the
mvn clean installcommand 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>/samplesdirectory.Execute the
antcommand 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. |
|
Joining a group chat |
|
Leaving a group chat |
|
Exiting the sample |
|
Example
Run two instances of this sample by running the
antor themvn clean installcommand from the<MB_HOME>/samples/MqttChatClientdirectory directory in two consoles.You will be requested for a chat alias in both instances. Enter
abcin one terminal anddefin 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 exitEnter the following command in the terminal of the
abcchat alias.
def Hello
The following will appear in the log of thedefchat alias.[java] >Personal message from abc [java] hello [java] Waiting for your input. Use <help> for more infoConnect both chat aliases to a group chat using the following command.
join Chat1GroupYou will get the following log for both instances.
[java] >Joined to the group : Chat1GroupEnter the command
leave Chat1Groupin the terminal forABCchat alias. The following would appear in the log.[java] >Left the group : Chat1GroupType
exitin the same terminal to stop the chat application.