Configuring WebSocket

Overview

WebSocket is a protocol that allows bi-directional web communication between client and server. You can configure your Maestro application to communicate via WebSocket using the method below.

Configuring for WebSocket

Maestro opens ports 8080 and 8443 by default on your servers to allow you to use WebSocket. To work with optimally Maestro, your WebSocket servers need to listen to these ports.

To configure your Maestro app to communicate with your WebSocket server, you need to set up your Service’s ports to allow your container to communicate via port 8080 (or 8443 for TLS).

For example, this service (ws-app) is running on port 3000, and communicating with WebSocket via 8080:

    version: 2
    services:
      ws-app:
        ports:
        - container: 3000
          http: 8080

Using custom ports for WebSocket

If necessary you can use different ports for WebSocket, but you will need to manually open those ports on Cloud 66 to allow external connections to your servers.

Testing a WebSocket connection

To test if your WebSocket server is connecting to your application correctly, you can create a .html file with the code below. Make sure to replace ** with your WebSocket server IP address and finally, open it in a web browser.

<html>
  <head>
    <script src="<http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js>"></script>
      <script>
      $(document).ready(function(){
        function debug( str ) {
          $("#debug").append( str );
        };

        ws = new WebSocket("ws://your address");
        ws.onmessage = function(evt) {
          $("#msg").append("evt.data");
        };
        ws.onclose = function() {
          debug("socket closed");
        };
        ws.onopen = function() {
          debug("connected...");
          ws.send("hello server");
        };
      });
    </script>
  </head>
  <body>
    <div id="debug"></div>
    <div id="msg"></div>
  </body>
</html>

WebSocket through a load balancer

You can use WebSocket if your application is using a load balancer, but you may need to make some configuration changes (depending on the load balancer). Cloud 66’s default load balancer is HAProxy.

Using WebSocket via HAProxy

By default, all HAProxy servers configured by Cloud 66 will redirect all WebSocket traffic from ports 80 or 443 to ports 8080 or 8443 of your web servers, so it should work with no additional configuration (assuming your WebSocket server uses the default ports).

To bypass this redirection by HAProxy, you can connect your WebSocket servers via ports 8080 and 8443. The HAProxy server is configured to pass through all traffic on ports 8080 and 8443 to the corresponding ports on the web servers.

Using WebSocket via native cloud load balancers

If you have manually set up a native load balancer with your cloud provider, and you are using it to distribute traffic to your application, you should consult your cloud provider’s documentation to ensure you’ve configured the load balancer to handle WebSocket traffic appropriately.