This is a C++ wrapper for the StreamrProxyClient library.
-
Obtain the StreamrProxyClient shared library by building the native-sdk project or by downloading the pre-built library from the StreamrProxyClient Releases page.
-
Open the shared library by linking it to your project or by loading it dynamically at runtime using
dlopen -
Include the StreamrProxyClient.hpp header file in your project.
-
See ../../examples/unix-cpp for a complete example project.
#include <cassert>
#include <iostream>
#include <string>
#include "StreamrProxyClient.hpp"
int main() {
// This is a widely-used test account
const std::string ownEthereumAddress =
"0xa5374e3c19f15e1847881979dd0c6c9ffe846bd5";
const std::string ethereumPrivateKey =
"23bead9b499af21c4c16e4511b3b6b08c3e22e76e0591f5ab5ba8d4c3a5b1820";
const std::string streamPartId =
"0xd2078dc2d780029473a39ce873fc182587be69db/low-level-client#0";
try {
// Create client instance
streamrproxyclient::StreamrProxyClient client(
ownEthereumAddress, ethereumPrivateKey, streamPartId);
// Connect to proxy
auto connectResult = client.connect(
{{.websocketUrl = "ws://95.216.15.80:44211",
.ethereumAddress =
"0xd0d14b38d1f6b59d3772a63d84ece0a79e6e1c1f"}});
assert(connectResult.errors.empty());
// Publish message
const std::string message = "Hello from libstreamrproxyclient C++!";
auto publishResult = client.publish(message);
assert(publishResult.errors.empty());
std::cout << ownEthereumAddress << " published message \"" << message
<< "\" to " << publishResult.successful.size()
<< " proxies\n";
} catch (const streamrproxyclient::StreamrProxyError& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}Main client class for interacting with Streamr proxies.
StreamrProxyClient(
const std::string& ownEthereumAddress,
const std::string& ethereumPrivateKey,
const std::string& streamPartId)Creates a new StreamrProxyClient instance.
Parameters:
ownEthereumAddress- Ethereum address of the clientethereumPrivateKey- Private key for the Ethereum addressstreamPartId- Stream part ID
Throws: StreamrProxyError if client creation fails
StreamrProxyResult connect(const std::vector<StreamrProxyAddress>& proxies)Connects to specified proxies.
Parameters:
proxies- Vector of proxy addresses to connect to
Returns: Result containing the proxies that the operation was successful on and the errors that occurred during the operation
StreamrProxyResult publish(const std::string& message)Publishes a message through connected proxies.
Parameters:
message- Message to publish
Returns: Result containing the proxies that the operation was successful on and the errors that occurred during the operation
Represents an error that occurred during a proxy operation.
Members:
StreamrProxyAddress proxy- Proxy address where the error occurredStreamrProxyErrorCode code- Error code
Methods:
std::string what()- Returns the error message
ErrorInvalidEthereumAddress- Indicates an invalid Ethereum addressErrorInvalidStreamPartId- Indicates an invalid stream part IDErrorProxyClientNotFound- Indicates proxy client was not foundErrorInvalidProxyUrl- Indicates an invalid proxy URLErrorNoProxiesDefined- Indicates no proxies were definedErrorProxyConnectionFailed- Indicates proxy connection failedErrorProxyBroadcastFailed- Indicates proxy broadcast failed
Represents a Streamr proxy address.
Members:
std::string websocketUrl- WebSocket URL of the proxystd::string ethereumAddress- Ethereum address of the proxy
Contains the results of a proxy operation.
Members:
std::vector<StreamrProxyAddress> successful- List of proxies that the operation was successful onstd::vector<StreamrProxyError> errors- List of errors that occurred during the operation