Updating AMPS Client Connections
Client Connection Strings
Use the URI that matches the proxy and AMPS transport configuration:
- Secure TCP connections:
tcps://localhost:443/tcps/amps/json?http_preflight=true - Secure WebSocket connections:
wss://localhost:443/wss/amps/json - Standard non-SSL TCP connections:
tcp://localhost:80/tcp/amps/json?http_preflight=true - Standard non-SSL WebSocket connections:
ws://localhost:80/ws/amps/json
The code examples below all use the secure URLs.
For a non-SSL deployment, replace tcps with tcp, wss with ws, port 443 with 80, and the /tcps/ and /wss/ paths with /tcp/ and /ws/.
WebSocket connections don't require the HTTP Preflight option as they use an HTTP Upgrade mechanism to upgrade to the WebSocket protocol by default.
For more information on HTTP Preflight, see HTTP Preflight in the AMPS User Guide.
Example Client Connections
- JavaScript
- Python
- C++
- Java
- C#/.NET
For install and usage details, see Obtaining and Installing the AMPS Client.
const { Client } = require('amps')
// required when the CA certificates are self-signed
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
async function main() {
// Connect and logon
const client = new Client('javascript-proxy-client')
// HTTP preflight is not required for standard websocket connections
await client.connect('wss://localhost:443/wss/amps/json')
console.log('connected!')
// No difference in usage once the client is connected
client.publish("test", "{\"hello\":\"world\"}")
client.disconnect()
}
main()
This example creates an AMPS Client and establishes a secure WebSocket connection to AMPS through the NGINX reverse proxy.
For install and usage details, see Obtaining and Installing the AMPS Client.
import AMPS
client = AMPS.Client('python-proxy-client')
# Connect and logon using HTTP preflight
client.connect('tcps://localhost:443/tcps/amps/json?http_preflight=true')
client.logon()
print('connected!')
# No difference in usage once the client is connected
client.publish("test", "{\"hello\":\"world\"}")
client.disconnect()
client.close()
This example creates an AMPS Client and uses HTTP Preflight to establish an upgraded TCPS connection to AMPS through the NGINX reverse proxy.
For install and usage details, see Obtaining and Installing the AMPS Client.
#include <amps/ampsplusplus.hpp>
#include <iostream>
int main()
{
AMPS::Client client("cpp-proxy-client");
// Connect and logon using HTTP preflight
client.connect("tcps://localhost:443/tcps/amps/json?http_preflight=true");
client.logon();
std::cout << "Connected!" << std::endl;
// No difference in usage once the client is connected
client.publish("test", "{\"hello\":\"world\"}");
client.disconnect();
return 0;
}
This example creates an AMPS Client and uses HTTP Preflight to establish an upgraded TCPS connection to AMPS through the NGINX reverse proxy.
For install and usage details, see Obtaining and Installing the AMPS Client.
SSL connections in Java require a properly configured truststore so the client can verify and trust the server’s certificate during the TLS handshake.
You can create a truststore by running the following:
keytool -importcert -file /etc/pki/tls/certs/localhost.crt -keystore amps-truststore.p12 -storepass 123456
import com.crankuptheamps.client.Client;
public class Example
{
public static void main(String[] args) {
// required for providing the proper certs for ssl connections
System.setProperty("javax.net.ssl.trustStore", "amps-truststore.p12");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
Client client = new Client("java-proxy-client");
try {
// Connect and logon using HTTP preflight
client.connect("tcps://localhost:443/tcps/amps/json?http_preflight=true");
client.logon();
System.out.println("Connected!");
// No difference in usage once the client is connected
client.publish("test", "{\"hello\":\"world\"}");
}
catch (Exception e) {
System.err.println("Exception: " + e);
} finally {
client.close();
}
}
}
This example creates an AMPS Client and uses HTTP Preflight to establish an upgraded TCPS connection to AMPS through the NGINX reverse proxy.
For additional SSL guidance, see Providing SSL Certificates to the AMPS Java Client.
For install and usage details, see Obtaining and Installing the AMPS Client.
using System;
using AMPS.Client;
using AMPS.Client.Exceptions;
try
{
// Connect and logon using HTTP preflight and the proxy endpoint
Client client = new Client("csharp-proxy-client");
client.connect("tcps://localhost:443/tcps/amps/json?http_preflight=true");
client.logon();
Console.WriteLine("Connected!");
// No difference in usage once the client is connected
client.publish("test", "{\"hello\":\"world\"}");
client.close();
}
catch (AMPSException exception)
{
Console.WriteLine(exception);
}
This example creates an AMPS Client and uses HTTP Preflight to establish an upgraded TCPS connection to AMPS through the NGINX reverse proxy.
If the clients fail to connect after following the above steps on Fedora/Red Hat, try running the following command:
sudo setsebool -P httpd_can_network_connect 1
This command modifies a security setting in SELinux (Security-Enhanced Linux) to allow the httpd (web server) process to make network connections. It also ensures that this setting persists across system reboots.
If you use the Fedora localhost certificate shown in this guide, connect to localhost rather than 127.0.0.1, or replace the certificate with one that matches the hostname clients will use.