DEV Community

Hong Qiu for Solace Developers

Posted on • Originally published at solace.com on

How to Build a Simple Chat App with Solace (Part 4)

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, I explained how to create a simple chat app that can send and receive messages via a direct topic subscription. In the second I explained how to modify the sample code so the application consumes messages from a queue, and in part 3 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

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.
  4. Log in to Solace PubSub+ Cloud.
  5. Click the Connect tab.
  6. Open the REST section.
  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.

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.
  2. Enter the following code to create a REST template object (Line 63).
//Rest Request goes hereRestTemplate restTemplate = new RestTemplate();
Enter fullscreen mode Exit fullscreen mode

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

  1. Enter the following code below the above code to create the HTTP entity with the user object type (Line 64).
HttpEntity<UserObject> request = new HttpEntity<UserObject>(userObject,httpHeaders);
Enter fullscreen mode Exit fullscreen mode

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

  1. 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);
Enter fullscreen mode Exit fullscreen mode

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.

  1. Enter the following code to return a response entity (Line 66).
return new ResponseEntity(HttpStatus.OK);
Enter fullscreen mode Exit fullscreen mode

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.
  3. In the Subscriber panel, click the Connect button, enter LOGIN/MESSAGE/REQUEST as the topic, and click the Subscribe button.
  4. Open a new browser and enter localhost:8081 to bring up the web application.
  5. Enter “user” as the username and password and click the Submit button. You should see the Try Me! tab has received the message.

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

The post How to Build a Simple Chat App with Solace (Part 4) appeared first on Solace.

Top comments (0)