Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/berndporr/fastcgi_json_cpp_api
Header-only JSON event driven communication between jQuery and C++ via nginx
https://github.com/berndporr/fastcgi_json_cpp_api
cpp fastcgi jquery json nginx real-time realtime
Last synced: 7 days ago
JSON representation
Header-only JSON event driven communication between jQuery and C++ via nginx
- Host: GitHub
- URL: https://github.com/berndporr/fastcgi_json_cpp_api
- Owner: berndporr
- License: apache-2.0
- Created: 2021-03-23T09:28:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-07T00:16:56.000Z (10 months ago)
- Last Synced: 2024-02-07T01:29:52.161Z (10 months ago)
- Topics: cpp, fastcgi, jquery, json, nginx, real-time, realtime
- Language: C++
- Homepage:
- Size: 1.03 MB
- Stars: 1
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](dataflow.png)
## 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](https://github.com/open-source-parsers/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](https://github.com/glasgowneuro/usbdux) 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](http://freedns.afraid.org/)