How to Build a Simple Chat App with Solace (Part 6)
Tags: solace, chat
Published: 2019-11-12 14:34:21

---
title: How to Build a Simple Chat App with Solace (Part 6)
published: true
date: 2019-10-30 19:08:22 UTC
tags: solace,chat
canonical_url: https://solace.com/blog/build-chat-app-solace-6/
series: "How to Build a Simple Chat App with Solace"
---

 ![Further thoughts on FPGA co-processing and performance](https://solace.com/wp-content/uploads/2019/08/solace-default-blog-thumbnail-600x285.jpg)

This is the sixth post in a series in which I walk through the creation of a simple chat app with Solace PubSub+. In [the last part](https://dev.to/hq190204/how-to-build-a-simple-chat-app-with-solace-part-5-12cm-temp-slug-8292081), I explained how to send a REST POST request as a request/reply message. In this final part, you will learn how to create the non-exclusive queue that will receive the login request and load balance them out to the authentication server. Specifically, you will:

- Create the non-exclusive queue
- Add the topic subscription to the new queue
- Modify the authentication server
- Test the load balancing functionality

### **Prerequisites**

- [Download Git](https://git-scm.com/downloads) to your computer
- [Install Apache Maven](https://maven.apache.org/install.html)
- [Sign up](https://console.solace.cloud/login) with Solace PubSub+ Cloud
- [Clone a repository](https://github.com/SolaceTraining/fundamentals-of-solace-dev) of the samples to your computer

### **Level**

- Beginner

## **Create the Non-Exclusive Queue**

You need to create a queue for our PubSub+ Cloud instance to receive and save the login request for the authentication server. To create the queue, follow the steps below:

1. In your code editor, check out the developer-exercise-5 branch or in your command line, enter `git checkout remotes/origin/bonus -f`
2. In your code editor, open the file LoginMessageReplier.java under auth-server > server.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-1.png)  
Line 50 contains the name of the queue “LOGIN\_QUEUE”.
3. [Log in](https://console.solace.cloud/login) to Solace PubSub+ Cloud, select your service, and click **Manage Service**.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-2-1024x113.png)
4. Click **Queues** in the left side.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-3-1024x301.png)
5. Click the **+ Create** button to create a new queue.
6. In the  **Create Queue** dialog box, name the queue LOGIN\_QUEUE and click **Create**.  
 **![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-4.png)**  
7. In the **Edit Queue Settings** dialog box, change the Access Type to Non-Exclusive and click the **Apply** button.  
  
Exclusive queues are for fault tolerance while non-exclusive queues are for load balancing.

[Learn about queue access types](https://docs.solace.com/PubSub-Basics/Endpoints.htm).

![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-5.png)

8. In the **Confirm** dialog box, click **Apply changes**.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-6.png)

## **Add the Topic Subscription to the Queue** 

1. Click on the new LOGIN\_QUEUE queue and click the **Subscription** tab.  
 **![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-7.png)**  
2. Click the  **+ Subscription** button to open the  **Create Subscription**  dialog box.
3. In the  **Create Subscription ** dialog box, add LOGIN/MESSAGE/REQUEST as the topic subscription, press  **Enter** , and click  **Create**.

## **Modify the Authentication Server**

You are going to modify the authentication server to consume from the login request instead of receiving messages via a topic subscription.

1. Under the auth-server directory, open the LoginMessageReplier.java file.
2. Enter the following code to set the endpoint properties. (Lines 90-91).
```
//Add the Queue Logic Herefinal EndpointProperties endpointProps = new EndpointProperties();
```
3. Enter the following code to set the queue permissions and access type (Lines 92-94).
```
//Set queue permissions to ‘consume’ and access type to ‘non-exclusive’endpointProps.setPermission(EndpointProperties.PERMISSION\_CONSUME);endpointProps.setAccessType(EndpointProperties.ACCESSTYPE\_NONEXCLUSIVE);
```
4. Enter the following code to create the queue object (Lines 96- 97).
```
//Create the queue object locallyfinal Queue queue = JCSMPFactory.onlyInstance().createQueue(QUEUE\_NAME);
```
5. Enter the following code to provision the queue to ensure that it binds to an existing queue (Lines 98-101).
```
//Actually provision the queue, and do not fail if it already existssession.provision(queue, endpointProps, JCSMPSession.FLAG\_IGNORE\_ALREADY\_EXISTS);System.out.printf(“Attempting to bind to the queue ‘%s’ on the PubSub+ Broker.%n”, QUEUE\_NAME);
```
6. Enter the following code to create consumer flow properties and the flow object (Lines 102-104).
```
//Create a Flow be able to bind to and consume messages from the Queuefinal ConsumerFlowProperties flowProps = new ConsumerFlowProperties();flowProps.setEndpoint(queue);
```

The consumer flow properties define the queue that you would like to connect to.

7. Enter the following code to set the acknowledgment mode for the flow (Line 105-107).
```
//Set to ‘auto acknowledge’ where the API will ack back to Solace at the end of the//message received callbackflowProps.setAckMode(JCSMPProperties.SUPPORTED\_MESSAGE\_ACK\_AUTO);
```

There are two types of acknowledgment modes: AUTO and CLIENT. Learn more about [acknowledgment modes](https://docs.solace.com/Solace-PubSub-Messaging-APIs/API-Developer-Guide/Acknowledging-Messages.htm).

8. Enter the following code to create the flow receiver and receive the messages (Lines 109-118).
```
final FlowReceiver cons = session.createFlow(new LoginRequestHandler(), flowProps, endpointProps, new FlowEventHandler() { @Override public void handleEvent(Object o, FlowEventArgs flowEventArgs) { System.out.println(o, toString() + “,” + flowEventArgs); } })//Start the consumercons.start();
```

This code snippet lets you handle events and print out the information to the console.

## Test the Load Balancing Functionality

With the load balancing consumption pattern, your applications are not blocked when your replying applications cannot keep up with the number of requests.

To test the load balancing functionality of the login server, follow these steps to start multiple authentication server instances and see how they load balance the login requests. Note that there should be one web-server instance and two auth-server instances running for the final test. Also make sure that you have the right environment variables.

1. In your code editor, type `mvn spring-boot:run` under the auth-server directory to run one instance of the authentication server of the Web application.
2. Open a new terminal in your code editor and type `mvn spring-boot:run` under the auth-server directory to run the second instance of the authentication server of the Web application.
3. Open two Web browsers and enter localhost:8081 for each of them to access the Web application.
4. In one of the instances of the Web application, enter the valid username “ValidUser” and password “solace”.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-8-1024x437.png)  
The login succeeds because this user is in the HashMapCredentialRepository.java file.
5. In the other instances of the Web application, enter the username “user” and password “user”.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-6_image-9.png)  
This time, the login fails because this user is not in the HashMapCredentialRepository.java file.

## Congratulations!

You have successfully added code to bind to the login queue and respond to the request messages.  
Not only that, you have reached the end of the series! To learn more about PubSub+ for developers, visit [solace.dev](https://www.solace.dev/). To share your developer experience using Solace PubSub+ or ask for help from other developers, visit the [Solace Community](https://solace.community/).

**Related**

- [How to Build a Simple Chat Application with Solace (Part 5)](https://dev.to/hq190204/how-to-build-a-simple-chat-app-with-solace-part-5-12cm-temp-slug-8292081)
- [How to Build a Simple Chat Application with Solace (Part 4)](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-4-35ei)
- [How to Build a Simple Chat Application with Solace (Part 3)](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-3-2657)
- [How to Build a Simple Chat Application with Solace (Part 2)](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-2-2efh)
- [How to Build a Simple Chat Application with Solace (Part 1)](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-1-3c9e)
- Udemy Course: [Fundamentals of Solace Development](https://www.udemy.com/fundamentals-of-solace-development/)

The post [How to Build a Simple Chat App with Solace (Part 6)](https://solace.com/blog/build-chat-app-solace-6/) appeared first on [Solace](https://solace.com).