How to Build a Simple Chat App with Solace (Part 5)
Tags: solace, chat
Published: 2019-11-12 14:33:37

---
title: How to Build a Simple Chat App with Solace (Part 5)
published: true
date: 2019-10-07 10:00:27 UTC
tags: solace,chat
canonical_url: https://solace.com/blog/build-chat-app-solace-5/
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 fifth of a series of blog posts in which I walk through the creation of a simple chat app with Solace PubSub+. In [the last part](https://dev.to/solacedevs/how-to-build-a-simple-chat-app-with-solace-part-4-35ei), I explained how to add a simple authentication server and send a REST POST request as a Publish/Subscribe type of message. In this tutorial, you will learn how to send a REST POST request as a request/reply message. Specifically, you will learn how to:

- Modify the sample code
- Connect the authentication server
- Change the login response message
- Test the authentication and response message

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

### **Modify the Sample Code**

The accompanying sample code for this tutorial is for sending a REST POST request as a Publish/Subscribe message. Since you will send a REST POST request as a Request/Reply message instead this time, you need to modify the sample code.

To modify the sample code, do the following:

1. In your code editor, check out the developer-exercise-5 branch or in your command line, enter `git checkout remotes/origin/developer-exercise-5 -f`
2. In your code editor, open the file `SolaceCloudProxy.java`  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-5_image-1.png)
3. Enter the following highlighted code to the section that determines a response to the Http Request to set the Solace reply wait time in milliseconds (Line 56).
```
//This header determines that the Http Request needs a responseset(“Solace-Reply-Wait-Time-In-ms”,”3000”);
```

The Solace-specific HTTP header “Solace-Reply-Wait-Time-In-ms” specifies that the message exchange pattern is Request/Reply and the Solace message broker application must wait for the specified timeout value (e.g. 3 seconds) for a reply before it stops.  
[Learn about Solace-Reply-Wait-Time-In-ms](https://docs.solace.com/RESTMessagingPrtl/Solace-REST-Message-Encoding.htm).

4. Modify the REST template POST request in the section that passes through a response code to expect a response of an authenticated object and to retrieve authenticatedObject from the call (Line 68).
```
//Pass through a response code based on the result of the REST-ful requestAuthenticatedObject authenticatedObject = restTemplate.postForObject(solaceRESTHost + “/LOGIN/MESSAGE/REQUEST”, request, AuthenticatedObject.class);
```

“AuthenticatedObject” is defined in the `AuthenticatedObject.java` file under solace-chat-common. It validates a client and determines whether to allow it to connect or not.

5. Under the above modified code line, enter the following code to define what message the client will get when their request is allowed or denied (Lines 70-73).
```
if (authenticatedObject.isAuthenticated()) { return new ResponseEntity(HttpStatus.OK);}else { return new ResponseEntity(HttpStatus.FORBIDDEN);}
```

### Connect the Authentication Server

To connect the authentication server to the PubSub+ instance, you need to define the connection details.

1. In the auth-server directory, copy and paste the `application-template.properties` file under src > main > resources.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-5_image-2.png)
2. Rename the copied file to `application.properties`.
3. [Log in](https://console.solace.cloud/login) to Solace PubSub+ Cloud and click the **Connect ** tab.
4. Open the Solace Messaging section.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-5_image-3.png)
5. From the Connection Details on the right, copy the following:
  - Username
  - Password
  - Message VPN
  - SMF Host
6. Paste the connection details to the `application.properties` file.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-5_image-4.png)

## Change the Login Response Message

When you receive a request to log in to the chat application, you need to authenticate the user and then send the response back to the application. To change the login response message, follow the steps below:

1. 
  1. In the auth-server directory, open the `LoginMessageReplier.java` file.  
 ![](https://solace.com/wp-content/uploads/2019/10/chat-app-part-5_image-5.png)
  2. Enter the following highlighted code to add the session subscription (Line 92).  
//Add the session subscription here  
`session.addSubscription(JCSMPFactory.onlyInstance().createTopic( REQUEST_TOPIC), true);`
  3. In the `createReplyMessage` method, enter the following highlighted code to create a message object for the reply message and then convert the Json text in the request message to a user object type (Line 110).
```
private XMLMessage createReplyMessage(TextMessage request) throws JCSMPException { TextMessage replyMessage = JCSMPFactory.onlyInstance().createMessage(TextMessage.class); UserObject userObject = gson.fromJson(request.getText(), UserObject.class);}
```
  4. Under the above code lines, enter the following highlighted code to validate the user and print out the message depending on the result of the validation (Lines 113 to 119).
```
private XMLMessage createReplyMessage(TextMessage request) throws JCSMPException { TextMessage replyMessage = JCSMPFactory.onlyInstance().createMessage(TextMessage.class); UserObject userObject = gson.fromJson(request.getText(), UserObject.class); //Validate the user boolean validUser = credentialsRepository.isValidUser(userObject.getUsername(), userObject.getPassword());if (validUser) System.out.println(“Successfully validated a user”);else System.out.println(“Authentication failed”);}
```
  5. Enter the following highlighted code to create an AuthenticatedObject type, set the reply message content type, set the text, and set the delivery mode (Lines 120 to 127).
```
if (validUser) System.out.println(“Successfully validated a user”);else System.out.println(“Authentication failed”);AuthenticatedObject authenticatedObject = new AuthenticatedObject();authenticatedObject.setAuthenticated(validUser);replyMessage.setHTTPContentType(“application/json”);replyMessage.setText(gson.toJson(authenticatedObject));replyMessage.setApplicationMessageId(request.getApplicationMessageId());replyMessage.setDeliverToOne(true);replyMessage.setDeliveryMode(DeliveryMode.DIRECT);return replyMessage;}
```

### Test the Authentication and Response Message

To test the authentication and login request response message, follow these steps:

  1. In your code editor, type `mvn spring-boot:run` to run the Web application.
  2. Open a new terminal window, change the directory to .\auth-server\, and type `mvn spring-boot:run` to run the authentication server.
  3. In your Web browser, type `localhost:8081` to view the application.
  4. Log in with a username and a password that are preset in the `HashMapCredentialRepository.java` file under the server directory in auth-server (e.g. ValidUser; solace).  
This should bring you to the chat application page.
  5. Refresh your browser and log in with an invalid username and an invalid password. You should see the “Login Failed” error message.

Congratulations! You now have a chat application that allows you to authenticate your user with a request/reply message pattern.

In the next part (coming soon), you will learn how to modify the Web application to load balance requests.

### Related

  - [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 5)](https://solace.com/blog/build-chat-app-solace-5/) appeared first on [Solace](https://solace.com).