I want to connect to the Push API of Poloniex. On their page they write the following:
In order to use the push API, connect to wss://api.poloniex.com
No, no, no this is a late response. Late or not though, I believe it may be a bit more direct a solution for you Bobface (and any others that struggled with this). I hesitate to give this out, as it will potentially be used by competitors. But, what's life without competition!? Boring, that's what. And moreover, I wish someone had come before me and posted this, so here you go! Be the change you wish to see, right?
Below, you'll find an implementation utilizing websocketpp and autobahn|cpp to connect to Poloniex's push api. In this case, it will receive updates made to the books for BTC_ETH.
Generally speaking, this is how you can utilize websocketpp and autobahn|cpp to connect to a secure web socket server implementing WAMP protocol (aka something like wss://ip-address.com:port).
Cheers!
Includes:
#include
#include
#include
#include
#include
#include
#include
#include
#include
Code:
typedef websocketpp::client client;
typedef autobahn::wamp_websocketpp_websocket_transport websocket_transport;
try {
//std::cerr << "Connecting to realm: " << parameters->realm() << std::endl;
boost::asio::io_service io;
//bool debug = parameters->debug();
client ws_client;
ws_client.init_asio(&io);
ws_client.set_tls_init_handler([&](websocketpp::connection_hdl) {
return websocketpp::lib::make_shared(boost::asio::ssl::context::tlsv12_client);
});
auto transport = std::make_shared < autobahn::wamp_websocketpp_websocket_transport >(
ws_client, "wss://api.poloniex.com:443", true);
// create a WAMP session that talks WAMP-RawSocket over TCP
auto session = std::make_shared(io, true);
transport->attach(std::static_pointer_cast(session));
// Make sure the continuation futures we use do not run out of scope prematurely.
// Since we are only using one thread here this can cause the io service to block
// as a future generated by a continuation will block waiting for its promise to be
// fulfilled when it goes out of scope. This would prevent the session from receiving
// responses from the router.
boost::future connect_future;
boost::future start_future;
boost::future join_future;
boost::future subscribe_future;
connect_future = transport->connect().then([&](boost::future connected) {
try {
connected.get();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
io.stop();
return;
}
std::cerr << "transport connected" << std::endl;
start_future = session->start().then([&](boost::future started) {
try {
started.get();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
io.stop();
return;
}
std::cerr << "session started" << std::endl;
join_future = session->join("realm1").then([&](boost::future joined) {
try {
std::cerr << "joined realm: " << joined.get() << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
io.stop();
return;
}
subscribe_future = session->subscribe("BTC_ETH", &on_topic1).then([&] (boost::future subscribed)
{
try {
std::cerr << "subscribed to topic: " << subscribed.get().id() << std::endl;
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
io.stop();
return;
}
});
});
});
});
std::cerr << "starting io service" << std::endl;
io.run();
std::cerr << "stopped io service" << std::endl;
}
catch (std::exception& e) {
std::cerr << "exception: " << e.what() << std::endl;
ret_var.successfully_ran = false;
return ret_var;
}