3

I have an underlying C application driving some pretty intensive hardware. I'm currently designing a web interface for the device, the majority of it's function is to present status information to the user. So every second or so, update fields on the web page with pertinent information (temperature, framerate, yada yada...) The device runs linux and has apache. Current setup:

  1. Javascript from the web status page makes an AJAX call (from setInterval)
  2. PHP file runs, writes to FIFO
  3. C application reads FIFO, does some work based on input (likely blank, meaning just give me the stats), writes back to FIFO
  4. PHP file reads from FIFO, parses, json_encode, echo
  5. Javascript parses JSON and updates page

Here's where you come in... the goal is to take out the PHP middleman. What are the conventions (assuming they exist) for AJAX calls to C programs? Best practice?

Options (as far as I know):

  • Sockets. Have the C program listen on a particular port. Something similar here.
  • Node.js. I'm not familiar with this, I've never used it. But my brief exposure makes me think this may be a great use for it? As I understand it... it's "javascript on the server side", but it's actually C code? Just need some insight here.
  • CGI. Can cgi work with a C program that constantly runs? Or does it load up a C program from scratch when called?
  • HTML5 WebSockets?

Any insight appreciated. Of course, the best answer is quite possibly not even listed above. That's just the extent of my knowledge in this area. Thanks!

Community
  • 1
  • 1
savinger
  • 6,544
  • 9
  • 40
  • 57
  • This is much better suited on a forum or chat room. We here at StackOverflow focus at solving concrete problems that you are stuck on. I'm not trying to say your question is stupid, bad or anything, it's just not really in the scope of StackOverflow. – orlp Sep 03 '12 at 19:11
  • Go the node.js way - it support WebSockets in order to prevent expensive `setInterval` polling. It's not C, it's pure js. – moonwave99 Sep 03 '12 at 19:12
  • 1
    @moonwave99 a fair amount of node is written in c++ ;) it's possible to write node extensions in c++, see http://www.lupomontero.com/writing-node-js-modules-in-cpp/ – Patrick Oscity Sep 03 '12 at 19:22
  • @padde oh like Objective-C++ for objc-c : very good to know, thank you! – moonwave99 Sep 03 '12 at 19:28
  • @moonwave99 well, with the difference that objective c does not need c++, whereas node is actually written in c++. You can rather compare it with a native c extension for ruby. – Patrick Oscity Sep 04 '12 at 10:50

1 Answers1

1

We have used the C library libwebsockets (http://libwebsockets.org/) to communicate between JavaScript and a C++ application. In our case we send JSON to and from the C++ backend via websockets.

In your case you could take the same data that you currently send via Ajax and send it to the C program over the websocket. The C program can then generate the same JSON that the PHP program generates and send that back to the JavaScript, again via the websocket.

Gerard Condon
  • 761
  • 1
  • 7
  • 22