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 and subscribe to the desired feed.
wss = WebSocket Secure -> SSL Protected
They also give an example for Node.js and Autobahn|JS:
var autobahn = require('autobahn');
var wsuri = "wss://api.poloniex.com";
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = function (session) {
function marketEvent (args,kwargs) {
console.log(args);
}
function tickerEvent (args,kwargs) {
console.log(args);
}
function trollboxEvent (args,kwargs) {
console.log(args);
}
session.subscribe('BTC_XMR', marketEvent);
session.subscribe('ticker', tickerEvent);
session.subscribe('trollbox', trollboxEvent);
}
connection.onclose = function () {
console.log("Websocket connection closed");
}
connection.open();
However, I do not want to use JavaScript, instead I use C++. There's also a Autobahn-Library for C++, called Autobahn|CPP. I've installed it and tried to run their subscriber example code with little modifications (basically just hardcoded the adress and port):
#include <autobahn/autobahn.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <memory>
#include <tuple>
void topic1(const autobahn::wamp_event& event)
{
std::cerr << "received event: " << event.argument<uint64_t>(0) << std::endl;
}
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
int main()
{
try {
boost::asio::io_service io;
boost::asio::ip::tcp::socket socket(io);
bool debug = true;
auto session = std::make_shared<
autobahn::wamp_session<boost::asio::ip::tcp::socket,
boost::asio::ip::tcp::socket>>(io, socket, socket, debug);
boost::future<void> start_future;
boost::future<void> join_future;
boost::asio::ip::tcp::endpoint rawsocket_endpoint( boost::asio::ip::address::from_string("173.236.42.218"), 443/*8000=standard*/);
socket.async_connect(rawsocket_endpoint,
[&](boost::system::error_code ec) {
if (!ec) {
std::cerr << "connected to server" << std::endl;
start_future = session->start().then([&](boost::future<bool> started) {
if (started.get()) {
std::cerr << "session started" << std::endl;
join_future = session->join("realm1").then([&](boost::future<uint64_t> s) {
std::cerr << "joined realm: " << s.get() << std::endl;
session->subscribe("ticker", &topic1);
});
} else {
std::cerr << "failed to start session" << std::endl;
io.stop();
}
});
} else {
std::cerr << "connect failed: " << ec.message() << std::endl;
io.stop();
}
}
);
std::cerr << "starting io service" << std::endl;
io.run();
std::cerr << "stopped io service" << std::endl;
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
There are a few things to explain here: I found out the IP-Adress 173.236.42.218 by simply pinging api.poloniex.com.
Port 443 is the standard SSL-Port. I've tried using the standard WAMP/WebSocket port which is 8000, but the server does not accept that. 80 is also not accepted.
So if I start the program, the output is the following:
starting io service
connected to server
Then, nothing happens. So the code must be stuck at session_start(), where the WS handshake is performed, what you can see when you look into wamp_session.ipp at line 80.
In my opinion the problem is that the API wants to use a secure connection (wss://). It seems like this piece of code does not try to create a SSL-encrypted connection and I don't know how to tell the session that I need a secure one.
Edit: In this question the Author says that Autobahn can't handle mixed http/wamp servers where and upgrade http-request is required first before using the WebSocket protocol. I know Poloniex uses such a mixed type, but I've tried to access the API with Autobahn|JS already and there it works fine, also sending the upgrade request. So maybe this is a Autobahn|CPP issue?
Edit 2: If the above is true, is it possible to send the Http-Update-Request myself and maybe even put a SSL-encryption onto the connection? I'm not sure because maybe that would interfere the library.
I know this is a rather late response to your question, but it appears the problem is you are not performing the HTTP/Websocket upgrade upon connecting to the remote server. The example code you are using is setup with the rawsocket_endpoint transport, which my guess means the there is no HTTP Websocket upgrade or Websocket encapsulation occurring. I do not believe your problem has anything to do with SSL.
To get Websocket connections to work you should review the AutobahnCPP example that makes use of Websocketpp.
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:
Code: