Quickstart: Query and Subscribe from a Client Program¶
This quickstart connects a Python script or a C++ program to a 3forge Enterprise instance, runs a SQL query, and receives a live update when a row changes.
The steps are shell commands and one program, in Python or C++. To publish from Java instead, start with the Java client.
Prerequisites¶
- A running 3forge Enterprise instance with its default security settings, and access to its installation directory, to edit
keys/3forge.authand restart the instance. - A user that can log in to the instance. The program below uses the default
demologin; replace it with a login of your own. - The
opensslandssh-keygencommand-line tools. Git for Windows and Ubuntu include both. - For Python: Python 3.14 and the
pyami-3forgepackage from the Python Package Index, installed withpip install pyami-3forge. - For C++: the client library, whose source 3forge provides as a download assigned to your account, built with a C++20 compiler, CMake 3.21 or later, and OpenSSL development files: OpenSSL 3 on Linux, OpenSSL 4.1 on Windows. See Building the library.
1. Create a client certificate¶
A default install accepts a client only if it presents a certificate whose public key is listed on the server.
-
In an empty working directory, create a private key and a certificate:
-
Write the certificate's public key as an
ssh-rsaline: -
Confirm that the key was written: run
cat client.pub, and the output is one line startingssh-rsa AAAA.
The key must be RSA: the server accepts no other key type.
2. Authorize the certificate on the instance¶
The server reads its list of allowed client keys only when it starts.
-
Append the line to the authorized-clients file, from the installation directory:
-
Restart the instance.
- Copy the server's public key,
keys/3forge.pub, into the working directory, so the program can check it is talking to this server. - Confirm that the key was added: run
tail -n 1 keys/3forge.auth, and the output is thessh-rsaline fromclient.pub. Stage 4 confirms that the server accepts it.
A key added without a restart is still refused
Adding a key, or removing one, takes effect only after a restart. Until then, a newly added key is rejected and a removed key is still accepted.
3. Write the program¶
The program opens the SQL connection and the Center connection, runs a query, subscribes to a table, and changes a row in it.
-
Save the program in the language you use:
Save the following code as
quickstart.pyin the working directory:Save the following code as
quickstart.cppin the working directory: -
For C++, compile the program against the library, replacing the
AMI_SRCpath with the library's source directory:Compile from a 64-bit prompt: the "x64 Native Tools Command Prompt" for Visual Studio 2026, or a "Developer Command Prompt for VS 2026" set up with
VsDevCmd.bat -arch=x64. The default prompt builds 32-bit programs, which fail to link against the 64-bit library withLNK4272andLNK1120: -
Confirm that the files are in place: run
ls, and the output lists the program,client.crt.pem,client.key.pem, and3forge.pub.
4. Run it¶
Running the program shows whether the server accepted the certificate, and the update arriving.
-
Run the program:
-
Check that the output shows the query result, then one update:
The
seqnumandidvalues differ from run to run.
The insert and the update arrive as one update, action 1 (added), carrying the updated price, because the server combines changes made to a row within one refresh. A later change arrives as action 2 (updated) with only the changed columns.
A refused certificate looks like a successful connection
If the server does not accept the certificate, add_cluster still returns without an error. The signs are lines in the log, such as Synchronous operation failed: ..., and an empty query result: {} in Python, or no table in C++. Check the result of SELECT 1, not the connection. See The client connects but queries return nothing.
Result¶
The program connects over mutual TLS, runs a query, subscribes to Quotes, and receives the row as an update. Next:
- Concepts explains clusters, the 3 connections, and the callback thread.
- The Python client and C++ client pages cover writing tables, publishing, and commands.
- Connecting and Securing a Client covers failover, timeouts, and the row limit.