Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • Federated identity across the hybrid cloud using zero trust workload identity manager

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.