Expose Websockets endpoint via 3scale API Manager
In this article, we will go through the steps needed to configure Websockets server as 3scale API backend and expose the Websockets server endpoint on API Gateway.
Setup your Environment -
- Setup Websockets server.
- Create 3scale Product in API Manager.
- Use Websocket client to test the Websocket endpoint.
Start Websockets server -
You can use any of your favorite frameworks to start Websockets server. Here we are using Node.js. Node.js installation is out of scope of this article.
Save the below script in a file like index.js. Start script using “node index.js”.
We will start Secured Websocket server using the below script :
// 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’);
});
Configure 3scale -
Step 1 : Add backend, create necessary metrics, Products and Application Plan as per 3scale doc to expose an endpoint.
Provide the Websocket server URL as below in the Private Base URL :
Step 2 : Add Websockets Policy to the Policy Chain. No configuration is needed inside the Policy.
Step 3 : Promote endpoint to Staging API Gateway for testing. Endpoint and mapping rules will look like below in the UI.
Step 4 : Launch chrome extension Pie Socket Tester. You can use any Websocket client. Use the staging API gateway URL and append Websocket Public Path before connecting.
