WebSockets with C++

WebSockets with C++

I have wrap libwebsockets (lws) in c++ classes to implement WebSockets on c++. It gives a simple way to work with lws and create a WebSocket connection with a few lines of code. The architecture of the lws is hierarchical. It has several protocols, and each protocol includes detail like address, path, destination port (443,9443), and WebSocket interface (WSI). A protocol can serve several services, where the users should subscribe to it. All the protocols are unified under the same context. I tried to create the use straightforward as it is in python. For example, see here for python implementation of WebStream to access real-time exchange rates stream.

How to use

The first step is to define WebSockets and to add the protocol. Here is example for Bitstamp and Binance the both are cryptocurrency exchange rates.

	WebSockets ws;
	ws.AddProtocol("bitstamp","ws.bitstamp.net","/",443);				
	ws.AddProtocol("binance","stream.binance.com","/ws",9443);
	ws.Connect();	

Use the following code to write to websocket :

ws["binance"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"btcusdt@trade\"],\"id\": 1}");
ws["binance"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"ltcusdt@trade\"],\"id\": 2}");
ws["binance2"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"xrpusdt@trade\"],\"id\": 3}");
ws["bitstamp"].Write("{ \"event\": \"bts:subscribe\",\"data\": { \"channel\": \"diff_order_book_btcusd\"}}");
ws["bitstamp2"].Write("{ \"event\": \"bts:subscribe\",\"data\": { \"channel\": \"diff_order_book_ltcusd\"}}");

Here is a complete example to get a live stream from Bitstamp and Binance:

int main(int argc, const char **argv)
{
	WebSockets ws;

	auto func = [](std::string json) ->bool {
		std::cout<<json<<std::endl;
		return true;
	};

	auto runAfterConnection = [&ws](void) ->void {
		ws["binance"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"btcusdt@trade\"],\"id\": 1}");
		ws["binance"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"ltcusdt@trade\"],\"id\": 2}");
		ws["binance2"].Write("{\"method\": \"SUBSCRIBE\",\"params\":[\"xrpusdt@trade\"],\"id\": 3}");
		ws["bitstamp"].Write("{ \"event\": \"bts:subscribe\",\"data\": { \"channel\": \"diff_order_book_btcusd\"}}");
		ws["bitstamp2"].Write("{ \"event\": \"bts:subscribe\",\"data\": { \"channel\": \"diff_order_book_ltcusd\"}}");
	};

	ws.AddProtocol("bitstamp","ws.bitstamp.net","/",443,func);				
	ws.AddProtocol("binance","stream.binance.com","/ws",9443,func);	
	ws.AddProtocol("binance2","stream.binance.com","/ws",9443,func);	
	ws.AddProtocol("bitstamp2","ws.bitstamp.net","/",443,func);				

	ws.Connect(runAfterConnection);		
	ws.Run();

}

Download & install libwebsockets

The project uses CMake as a build system, and it builds the lws, it could refer on my previous post. So here is how to build lws in CMake.

	ExternalProject_Add(websockets_external
		GIT_REPOSITORY https://github.com/warmcat/libwebsockets.git
		GIT_TAG v3.2.0
		CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DHAVE_SSL=ON -DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}/${3rd_part}/websockets/${CMAKE_BUILD_TYPE}/
		DEPENDS websockets
		)

Download the project from here:

git clone git@github.com:yairgd/websockets.git

Ans use this to build it:

cd websockets &&  mkdir Debug && cmake -DCMAKE_BUILD_TYPE=Debug ..
Comments
comments powered by Disqus