Giter VIP home page Giter VIP logo

fastcgi_json_cpp_api's Introduction

jQuery <--> C++ JSON communication

C++ Header-only event driven communication between jQuery in a web-browser via the nginx web-server. This can implement, for example, a RESTful API for fast sensor data transfer.

This was developed because of a lack of a lightweight jQuery to C++ communication. It's a small helper which can easily be included in any C++ application which needs to talk to a web page where realtime data needs to be exchanged.

Works alongside PHP as nginx can serve both!

alt tag

Prerequisites

apt-get install libfcgi-dev
apt-get install libjsoncpp-dev
apt-get install nginx-core

Installation

This is a pure header based library. All the code is in json_fastcgi_web_api.h. Just type:

sudo make install json_fastcgi_web_api.h

to install the header in the system-wide include dir.

Howto

The only file you need is:

json_fastcgi_web_api.h

The file json_fastcgi_web_api.h has extensive inline documentation. Its doxygen generated online documentation is here: https://berndporr.github.io/fastcgi_json_cpp_api/

Implement the GET callback (server -> client)

This is the callback which sends JSON packets to the client (website, phone app, etc):

	class GETCallback {
	public:
		/**
		 * Needs to return the JSON data sent to the web browser.
		 **/
		virtual std::string getJSONString() = 0;
	};

Overload getJSONString() and return JSON. The recommended way of generating JSON is with the jsoncpp library which is part of all major Linux distros.

Implement the POST callback (client -> server, optional)

This handler receives the JSON from jQuery POST command from the website for example when the user presses a button. Implement the callback:

	class POSTCallback {
	public:
		/**
		 * Receives the data from the web browser in JSON format.
		 **/
		virtual void postString(std::string arg) = 0;
	};

Overload postString(std::string arg) with a function which decodes the received POST data.

Start the communication

The start method takes as arguments the GET callback, the POST callback and the path to the fastCGI socket:

JSONCGIHandler jsoncgihandler;
jsoncgihandler.start(GETCallback* argGetCallback,
                     POSTCallback* argPostCallback = nullptr,
		     const char socketpath[] = "/tmp/fastcgisocket");

Stop the communication

Just call jsoncgihandler.stop() to shut down the communication.

Example code

Fake sensor demo

The subdir fake_sensor_demo contains a demo_sensor_server which fakes a temperature sensor and its readings are plotted in a web browser. The nginx config file and the website are in the website directory.

Real ADC ADS1115 realtime demo

The subdir ads1115_demo contains a demo where the channel one from a real ADS1115 ADC chip is plotted in a web browser. You need an ADS1115 ADC for this demo.

Real USB-DUX realtime demo

The subdir usbdux_demo contains a demo where the temperature from an LM35 is digitised with a USB-DUX and then plotted in a web browser. You need an USB-DUX (sigma or D) for this demo.

Credit

Bernd Porr, [email protected]

Dynamic DNS

if you have no static IP address you can use the the "afraid" dynamic DNS service: Free DNS

fastcgi_json_cpp_api's People

Contributors

arsenic-atg avatar berndporr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

fastcgi_json_cpp_api's Issues

Error while using older Cmake version

One of my teammates tried to build the project in an older Cmake version (3.5.1) and got the following error:

 INTERFACE_LIBRARY targets may only have whitelisted properties.  The
  property "PUBLIC_HEADER" is not allowed.

Apparently, this was a requirement prior to CMake 3.19 update. Link to a related thread on cmake forums talking about the issue. Attaching a screenshot of the error.

Screenshot from 2022-04-08 04-30-34

Typo detected in README.md

These are some typos that I found in your README.md file using codespell tool

README.md:21: libary ==> library
README.md:110: brower ==> browser

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.