Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Exposing WebSocket endpoints via 3scale API Management

July 1, 2021
Srikanth Valluru
Related topics:
APIsNode.js
Related products:
Red Hat 3scale API ManagementRed Hat OpenShift

Share:

    WebSocket is a communications protocol that provides full-duplex communication channels to web servers and clients over a single TCP connection. The protocol was standardized by the World Wide Web Consortium (W3C) and has been in common use by web developers for more than a decade.

    Red Hat 3scale API Management is a hosted environment for web applications. In this quick tip, you will see how to use 3scale to set up WebSocket communication easily. Figure 1 shows how 3scale mediates between the web client and the WebSocket interface on the server.

    The 3scale WebSockets policy stands between the client and server.
    Figure 1: The relationship between the browser, 3scale, and the server.

    This tip takes you through the following steps:

    • Setting up the WebSocket server.
    • Configuring 3scale API Management.
    • Using a WebSocket client to test the WebSocket endpoint.

    Step 1: Set up the WebSocket server

    You can use any of your favorite frameworks to start the WebSocket server. For this article, we use Node.js. (Installing Node.js is out of the scope of this tip.)

    We'll also use a simple JavaScript program that sets up a WebSocket server, accepts a request, and sends a reply. You can save it as index.js:

    // Minimal amount of secure websocket server
    
    var fs = require('fs');
    
    // read ssl certificate
    
    var privateKey = fs.readFileSync('ssl-cert/key.pem', 'utf8');
    
    var certificate = fs.readFileSync('ssl-cert/certificate.pem', 'utf8');
    
    var credentials = { key: privateKey, cert: certificate };
    
    var https = require('https');
    
    //pass in your credentials to create an https server
    
    var httpsServer = https.createServer(credentials);
    
    httpsServer.listen(8443,"0.0.0.0");
    
    var WebSocketServer = require('ws').Server;
    
    var wss = new WebSocketServer({
    
        server: httpsServer
    
    });
    
    wss.on('connection', function connection(ws) {
    
        ws.on('message', function incoming(message) {
    
            console.log('received: %s', message);
    
            ws.send('reply from server : ' + message)
    
        });
    
        ws.send('something');
    
    });

    You can use Node.js to start the script:

    $ node index.js

    Step 2: Configure 3scale API Management

    Follow the 3scale documentation to add a back end and create the necessary metrics, products, and application plan to expose an endpoint. Provide the WebSocket server URL as the Private Base URL, as shown in Figure 2.

    Enter the WebSocket server URL as the Private Base URL.
    Figure 2: Enter the WebSocket server URL in the Private Base URL field.

    Add your WebSockets policy to the policy chain, as shown in Figure 3. No configuration is needed inside the policy.

    Using the 3scale dialog to define the policy chain.
    Figure 3: Configuring the policy chain in 3scale.

    Promote the endpoint to the staging API Gateway for testing. Figure 4 shows how the endpoint and mapping rules appear in the console.

    Viewing the server's endpoint and mapping rules in the console.
    Figure 4: View the endpoint and mapping rules in the console.

    Step 3: Use a WebSocket client to test the WebSocket endpoint

    A convenient client we use for testing in this example is the Chrome browser's Web Socket Client extension. Enter the staging API Gateway URL and append the WebSocket public path to connect, as shown in Figure 5.

    Testing a 3scale WebSocket connection by entering a URL.
    Figure 5: A sample URL for testing a 3scale WebSocket connection.

    Conclusion

    3scale API Management offers policies to support communication between your front end and back end. See these resources for further information:

    • WebSocket policy in 3scale
    • WebSocket protocol support for APIcast
    • Supported Policies in 3scale
    Last updated: September 19, 2023

    Related Posts

    • How to visualize 3scale API Management analytics data

    • Custom policies in Red Hat 3scale API Management, Part 1: Overview

    • Packaging APIs for consumers with Red Hat 3scale API Management

    Recent Posts

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    • Confidential VMs: The core of confidential containers

    • Benchmarking with GuideLLM in air-gapped OpenShift clusters

    • Run Qwen3-Next on vLLM with Red Hat AI: A step-by-step guide

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue