Using WebSocket with Java in Azure App Service
Published Feb 20 2022 06:10 PM 8,602 Views
Microsoft

Azure App Service supports using WebSocket protocol in both Windows and Linux Web Apps. Developers can enable and use WebSockets in their applications by turning on WebSockets feature in App Service Configuration -> General Settings portal.

 

This blog walks you through the process of creating a Java WebSocket sample application in both Windows and Linux App Service.

 

We will use a simple Java Spring Boot Websocket sample to build a server that accepts a message that carries a user’s name. In response, the server will push a greeting into a queue to which the client is subscribed.

 

Hanli_Ren_0-1644737250054.gif

 

Prerequisite

  1. Azure account with an active subscription
  2. JDK 1.8 or later
  3. Maven 3.2+
  4. Git client

 

Build & Test the Java Spring Boot Websocket Service

1.  Git clone the sample code 

 

git clone https://github.com/spring-guides/gs-messaging-stomp-websocket.git

 

 

2.  cd into gs-messaging-stomp-websocket/complete folder
The folder container all the source code, pom.xml and maven command file that required to build the Websocket server.

 

PS D:\> cd xxx\gs-messaging-stomp-websocket\complete
	
PS D:\xxx\gs-messaging-stomp-websocket\complete> ls
   Directory: D:\xxx\gs-messaging-stomp-websocket\complete
	
	Mode                 LastWriteTime         Length Name
	----                 -------------         ------ ----
	d-----         2/11/2022   1:21 PM                .mvn
	d-----         2/11/2022   1:21 PM                gradle
	d-----         2/11/2022   1:21 PM                src
	d-----         2/11/2022   3:00 PM                target
	-a----         2/11/2022   1:21 PM             13 .gitignore
	-a----         2/11/2022   1:21 PM            682 build.gradle
	-a----         2/11/2022   1:21 PM           5774 gradlew
	-a----         2/11/2022   1:21 PM           2763 gradlew.bat
	-a----         2/11/2022   1:21 PM           9895 mvnw
	-a----         2/11/2022   1:21 PM           6301 mvnw.cmd
	-a----         2/11/2022   2:52 PM           3303 pom.xml
	-a----         2/11/2022   1:21 PM             47 settings.gradle

 

 

Notes:
We are not focusing on how to build WebSocket application in Java in this blog, so we jump right to the completed source code.

For more details about how to use STOMP messaging with Spring to create an interactive web application, please refer to:
https://spring.io/guides/gs/messaging-stomp-websocket/#initial 

 

3. Build an executable JAR

 

PS D:\xxx\gs-messaging-stomp-websocket\complete> .\mvnw clean package

 

The executable jar file will be generated under gs-messaging-stomp-websocket\complete\target\ folder.

 

4. Test the service

 

PS D:\xxx\gs-messaging-stomp-websocket\complete> java -jar .\target\messaging-stomp-websocket-complete-0.0.1-SNAPSHOT.jar

 

Open your browser at http://localhost:8080, you should be able to use "Connect" to open a connection, then send message and get response from the server.

 

Deploy to Linux App Service

1. Create a Linux App Service, or using an existing Linux App Service.
Go to Configuration -> General setting portal
Select language stack as Java
Select Java web server as Java SE(Embedded Web Server)
Enable Web sockets

Hanli_Ren_0-1644735579535.png

 

2. Run the Maven command below to configure the deployment. This command will help you to select the Azure App Service to deploy the java application.

mvn com.microsoft.azure:azure-webapp-maven-plugin:2.3.0:config

 

PS D:\xxx\gs-messaging-stomp-websocket\complete> mvn com.microsoft.azure:azure-webapp-maven-plugin:2.3.0:config
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.example:messaging-stomp-websocket-complete >-----------
[INFO] Building messaging-stomp-websocket-complete 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:2.3.0:config (default-cli) @ messaging-stomp-websocket-complete ---
Auth type: AZURE_CLI
Default subscription: xxxxx(xxxxxxxxxxxxxxx)
Username: xxxxx@xxxx
[INFO] Subscription: xxxxx(xxxxxxxxxxxxxxx)
[INFO] It may take a few minutes to load all Java Web Apps, please be patient.
[INFO] Reflections took 154 ms to scan 6 urls, producing 24 keys and 340 values
Java SE Web Apps in subscription hanren:
* 1: <create>
  2: ***LinuxJavaSE (Linux|Java 11|Java SE)
  3: ***winJavaSE (Windows|Java 11|Java SE)
Please choose a Java SE Web App [<create>]: 2
Please confirm webapp properties
Subscription Id : xxxxxxxxxxxxxxx
AppName : ***LinuxJavaSE
ResourceGroup : xxxxxx
Region : xxx
PricingTier : P2v2
OS : Linux
Java : Java 11
Web server stack: Java SE
Deploy to slot : false
Confirm (Y/N) [Y]: y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:49 min
[INFO] Finished at: 2022-02-13T12:24:15+08:00
[INFO] ------------------------------------------------------------------------

 

 

3. Deploy the Java application to Web App.

mvn package azure-webapp:deploy

 

PS D:\xxx\gs-messaging-stomp-websocket\complete> mvn package azure-webapp:deploy

...

[INFO] --- azure-webapp-maven-plugin:2.3.0:deploy (default-cli) @ messaging-stomp-websocket-complete ---
Auth type: AZURE_CLI
Default subscription: xxxxx(xxxxxxxxxxxxxxx)
Username:  xxxxx@xxxx.xxx
[INFO] Subscription: xxxxx(xxxxxxxxxxxxxxx)
[INFO] Updating target Web App ***LinuxJavaSE...
[INFO] Successfully updated Web App ***LinuxJavaSE.
[INFO] Trying to deploy external resources to ***LinuxJavaSE...
[INFO] Successfully deployed the resources to ***LinuxJavaSE
[INFO] Trying to deploy artifact to ***LinuxJavaSE...
[INFO] Deploying (D:\xxxx\gs-messaging-stomp-websocket\complete\target\messaging-stomp-websocket-complete-0.0.1-SNAPSHOT.jar)[jar]  ...
[INFO] Successfully deployed the artifact to https://xxxx.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:57 min
[INFO] Finished at: 2022-02-13T12:43:00+08:00
[INFO] ------------------------------------------------------------------------

 

 

After the jar file being successfully deployed to the Web App, you can go to https://<linux-webapp-name>.scm.azurewebsites.net/webssh/host.

Under the /home/site/wwwroot folder, there should be an app.jar file contains all the compiled code of your application.

Hanli_Ren_4-1644736092620.png

 

4. Test the service
Open your browser at https://<linux-webapp-name>.azurewebsites.net/, you should be able to use "Connect" to open a connection, then send message and get response from the server with multiple clients.

Hanli_Ren_1-1644735289406.png

 

Deploy to Windows App Service

1. Create a Linux App Service, or using an existing Linux App Service.
Go to Configuration -> General setting portal
Select language stack as Java
Select Java web server as Java SE(Embedded Web Server)
Enable Web sockets

Hanli_Ren_2-1644735672935.png

 

2. Run the Maven command below to configure the deployment. This command will help you to select the Azure App Service to deploy the java application.

 

 

 PS D:\xxx\gs-messaging-stomp-websocket\complete> mvn com.microsoft.azure:azure-webapp-maven-plugin:2.3.0:config
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.example:messaging-stomp-websocket-complete >-----------
[INFO] Building messaging-stomp-websocket-complete 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:2.3.0:config (default-cli) @ messaging-stomp-websocket-complete ---
Auth type: AZURE_CLI
Default subscription: xxxxx(xxxxxxxxxxxxxxx)
Username: xxxxx@xxxx.xxx
[INFO] Subscription: xxxxx(xxxxxxxxxxxxxxx)
[INFO] It may take a few minutes to load all Java Web Apps, please be patient.
[INFO] Reflections took 154 ms to scan 6 urls, producing 24 keys and 340 values
Java SE Web Apps in subscription hanren:
* 1: <create>
  2: ***LinuxJavaSE (Linux|Java 11|Java SE)
  3: ***winJavaSE (Windows|Java 11|Java SE)
Please choose a Java SE Web App [<create>]: 3
Please confirm webapp properties
Subscription Id : xxxxxxxxxxxxxxx
AppName : ***winJavaSE
ResourceGroup : xxxxxx
Region : xxx
PricingTier : P2v2
OS : Windows
Java : Java 11
Web server stack: Java SE
Deploy to slot : false
Confirm (Y/N) [Y]: y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:40 min
[INFO] Finished at: 2022-02-13T12:45:15+08:00
[INFO] ------------------------------------------------------------------------

 

 

After the jar file being successfully deployed to the Web App, you can go to https://<window-webapp-name>.scm.azurewebsites.net/DebugConsole 

Under the D:/home/site/wwwroot folder, there should be an app.jar file contains all the compiled code of your application.

https://rhlwinjavase.scm.azurewebsites.net/DebugConsole

Hanli_Ren_5-1644736428842.png

 

4. Test the service

Open your browser at https://<windows-webapp-name>.azurewebsites.net/, you should be able to use "Connect" to open a connection, then send message and get response from the server with multiple clients.

Hanli_Ren_3-1644735776358.png

 

Co-Authors
Version history
Last update:
‎Feb 20 2022 11:09 PM
Updated by: