WebSocket+STOMP Proxy Tutorial
We will demonstrate how to setup Membrane Service Proxy as a proxy for HTTP requests as well as WebSocket connections. We will use ActiveMQ as a backend to handle WebSocket connections. All non-WebSocket HTTP traffic will be handled by a static website in this example.
1. Get Started
Download Membrane Service Proxy version 4.1.0 or higher, unzip it and go to the extracted folder examples\websocket-stomp. Double click service-proxy.bat to start Membrane Service Proxy.
If the console window almost immediately closes again and Membrane Service Proxy already stopped, you need to fix a bug in the 4.1.0 example configuration: Replace the content of the file proxies.xml with the one listed below the screenshot. Then restart Membrane Service Proxy.
Inspect the initial configuration in the proxies.xml file you just activated:
<spring:beans xmlns="http://membrane-soa.org/proxies/1/" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://membrane-soa.org/proxies/1/ http://membrane-soa.org/schemas/proxies-1.xsd"> <router> <serviceProxy port="4443"> <ssl> <keystore location="../../conf/membrane.jks" password="secret" keyPassword="secret" /> <truststore location="../../conf/membrane.jks" password="secret" /> </ssl> <log /> <webSocket url="http://localhost:61614/" /> <target host="localhost" port="4444" /> </serviceProxy> <serviceProxy port="4444"> <webServer docBase="." index="index.html" /> </serviceProxy> <serviceProxy name="Console" port="9000"> <basicAuthentication> <user name="admin" password="membrane" /> </basicAuthentication> <adminConsole /> </serviceProxy> </router> </spring:beans>
2. Running ActiveMQ
Download Apache ActiveMQ 5.12 from http://activemq.apache.org/activemq-5120-release.html and unpack it. Go to the extracted directory bin\win64 and start activemq.bat.
If the console window closes almost immediately and ActiveMQ already stopped, one of ActiveMQ's ports is probably occupied. On Windows, you can stop the Internet Connection Sharing (ICS) service by pressing Ctrl+Alt+Del, starting the Task Manager, switching to the Services tab, right-click Internet Connection Sharing and choose Stop. Then restart ActiveMQ. Once ActiveMQ is started, you can start the ICS service again.
3. Opening the Website
Open https://localhost:4443/ in a browser.
As you are using Membrane's default certificate, you will get a message This Connection is Untrusted. Click I understand the Risks, Add Exception and Confirm Security Exception.
You will see an empty page with the string "Messages:" on it.
4. Sending a message
Open http://localhost:8161/ in a new browser window.
Choose Manage ActiveMQ broker and enter the username admin and password admin. Open the Queues tab.
Look for the foo row and choose Send To.
Replace the message body with abcd and hit Send.
Observe that in the other browser window, the message arrived instantly.
5. Final comments
You have successfully used Membrane Service Proxy as a WebSocket Proxy. This can help you with Web Service Integration, Service Virtualization, realization of an API Gateway and the Implementation of a Security Layer.
The WebSockets protocol can, of course, be used for much more that 'just' communication with a HTML page: It allows arbitrary data to be passed bi-directionally over any open connection.