Skip to content

andrei-markeev/ddpserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DDP Server for C++

This C++ library implements server endpoint for Meteor's DDP protocol v1.

Use cases

  • Provide a custom live data source from your C++ application to Meteor frontend
  • Call your C++ methods from Meteor

Dependencies

  • CMake 2.8+
  • jvar - low-memory JSON serialization library (will be fetched and built automatically by CMake)

Build & install

Use

DdpServer only processes DDP protocol itself. It should be used with some websocket library. Examples of integrating with following libraries are available:

Other libraries can be integrated similarly.

Example below uses websocketpp.

C++:

jvar::Variant myMethod1(void *context, jvar::Variant& args)
{
	return args[0] + args[1];
}

void emitCallback(void *context, const std::string &output)
{
    websocketpp::connection_hdl hdl = *(websocketpp::connection_hdl *)context;
    wsServer->send(hdl, output.c_str(), output.length(), websocketpp::frame::opcode::text);
}

void websocket_on_message(websocketpp::connection_hdl hdl, websocket_server::message_ptr msg)
{
    ddpServer->setContext(&hdl);
    ddpServer->process(msg->get_payload());
}

// .. skipped ..

int main(int argc, char** argv)
{
	ddpServer = new DdpServer(emitCallback);
	ddpServer->registerMethod("myMethod1", myMethod1);

	// .. skipped ..
}

JavaScript (client-side):

MyConnection = DDP.connect("http://localhost:9002");

MyConnection.call("myMethod1", "param1", "param2", (error, result) => {
	if (error)
		alert(error.reason);
	else
		console.log(result);
});

Result:

screenshot

Live data

It is also possible to implement Meteor live data:

C++:

jvar::Variant data;
data.createObject();
data.addProperty("banana", "value");
data.addProperty("tomato", 1234);
data.addProperty("kiwi", "something");
ddpServer->emitAdd("coll1", "element1_id", data);

// ...

jvar::Variant changeData;
changeData.createObject();
changeData.addProperty("banana", "new_value");
ddpServer->emitChange("coll1", "element1_id", changeData);

// ...

ddpServer->emitRemove("coll1", "element1_id");	

JS:

Collection1 = new Mongo.Collection("coll1", { connection: MyConnection });

Subscriptions aren't supported for now, all emits will be immediately published to the frontend. Subscriptions are planned, but for now you can easily emulate them using methods.

Meteor auth integration

It is possible to integrate Meteor authentication with DDP server. Meteor stores loginToken on client. Idea is to pass it along with the user id to C++ application, and then verify it by performing a separate request to Meteor REST API:

Example code:

Project roadmap

  • Subscriptions support

About

Server endpoint for Meteor's DDP protocol in C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published