How to Build a Simple Chat App with Solace (Part 4)
Tags: solace, chat
Published: 2019-10-07 14:31:59

---
title: How to Build a Simple Chat App with Solace (Part 4)
published: true
tags: solace,chat
canonical_url: https://solace.com/blog/build-chat-app-solace-4/
series: "How to Build a Simple Chat App with Solace"
---

 ![](https://solace.com/wp-content/uploads/2019/07/chat-app-blog-featured-image.jpg) 

This is part 4 of a series of blog posts in which I walk through the creation of a simple chat app with Solace PubSub+. In [the first part](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-1-3c9e), I explained how to create a simple chat app that can send and receive messages via a direct topic subscription. In [the second](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-2-2efh) I explained how to modify the sample code so the application consumes messages from a queue, and in [part 3](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-3-2657) I explained how to add login functionalities to send a REST POST request to Solace PubSub+.

In this tutorial, you will learn how to add a simple authentication server. The server will first receive the client login request from the web application, then authenticate the user, and finally send a REST POST response back to the application to either allow or deny the login request.

Specifically, you will

- Set the connection details
- Create and send the REST POST request
- Test the REST POST request

**Prerequisite**

- [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

## **Set the Connection Details**

The setting of the connection details tells the Web application server where to send the REST POST login request to so that the Solace instance can receive it.

To set the connection details, do the following:

1. In your code editor, check out the developer-exercise-4 branch or in your command line, enter `git checkout remotes/origin/developer-exercise-4 -f`
2. In your code editor, enter `mvn install` to run a Maven install in solace-chat-common.
3. Under src > main > resources, open the file `application.properties`.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-1.png)
4. [Log in](https://console.solace.cloud/login) to Solace PubSub+ Cloud.
5. Click the **Connect** tab.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-2.png)
6. Open the REST section.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-3.png)
7. Copy the username, password, and REST host information from the Connection Details section and paste them to the application.properties file under src > main > java > resources.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-4.png)

## **Create and Send the REST POST Request** 

Now the connection details are set, you can create and send the REST POST request to the PubSub+ instance as a Publish/Subscribe message:

1. In your code editor, under src > main > java, open the file SolaceCloudProxy.java.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-5.png)
2. Enter the following code to create a REST template object (Line 63).
```
//Rest Request goes hereRestTemplate restTemplate = new RestTemplate();
```

`RestTemplate` makes HTTP REST calls and simplifies the interaction with HTTP servers.

3. Enter the following code below the above code to create the HTTP entity with the user object type (Line 64).
```
HttpEntity&lt;UserObject&gt; request = new HttpEntity&lt;UserObject&gt;(userObject,httpHeaders);
```

The user authentication type contains just a username and password. solace-chat-common has the reference to it.

4. Enter the following code to add the POST for the object to the REST template (Line 65).
```
restTemplate.postForObject(solaceRESTHost + “/LOGIN/MESSAGE/REQUEST”,request, String.class);
```

You defined `solaceRESTHost` at Step 7 of the “Set the Connection Details” section above.  
`/LOGIN/MESSAGE/REQUEST` is the specific topic that you are sending the REST POST request to.

5. Enter the following code to return a response entity (Line 66).
```
return new ResponseEntity(HttpStatus.OK);
```

`ResponseEntity` creates an HTTP response.

## **Test the REST Request**

Now you have created and sent the REST POST request, you can test whether the request is actually delivered to the Solace instance.

  1. In your code editor, enter `mvn spring-boot:run` to start the web application.
  2. In Solace PubSub+ Cloud, click the **Try Me!** tab.  
 **![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-6.png)**  
  3. In the Subscriber panel, click the **Connect** button, enter **LOGIN/MESSAGE/REQUEST** as the topic, and click the **Subscribe** button.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-7.png)
  4. Open a new browser and enter `localhost:8081` to bring up the web application.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-8.png)
  5. Enter “user” as the username and password and click the **Submit** button.  
You should see the Try Me! tab has received the message.  
 ![](https://solace.com/wp-content/uploads/2019/09/chat-app-part-4_image-9.png)

Congratulations! You have successfully added an authentication server, sent a REST POST request to the server as a Publish/Subscribe message, and received a response from it. In the next part (coming soon), you will learn how to send the REST POST request as a Request/Reply message.

**Related**

  - [How to Build a Simple Chat App with Solace (Part 3)](https://solace.com/blog/build-chat-app-solace-3)
  - [How to Build a Simple Chat App with Solace (Part 2)](https://solace.com/blog/build-chat-app-solace-2)
  - [How to Build a Simple Chat App with Solace (Part 1)](https://solace.com/blog/build-chat-app-solace-1)
  - 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 4)](https://solace.com/blog/build-chat-app-solace-4/) appeared first on [Solace](https://solace.com).