I would like to create a web application (meaning that will run from browser). The point is that I want to write just the UI in HTML5 + CSS + JS (or maybe even some PHP). On the other hand, I want to write the functionality of the application in C or C++. Then, I want to combine the code about the UI and functionality. Therefore, I would like to ask, whether it is possible to combine C/C++ code with web markup/programming languages?
8
There are a couple ways of using C or C++ in the web world.
The ancient method is CGI. With this approach you have a web server that is configured to launch CGIs fork
and exec
the CGI when requested. The problem with this is that the fork and exec is rather heavy weight. If you have 1000 simultaneous connections, it means you have 1000 processes running – not ideal.
Moving a step closer to the web server, in particular apache this time, one could write a module for it in C which handles the code. This lives within the web server as a library and is much faster, though it means tightly integrating with the apache environment which isn’t always trivial. The most recent book for this approach was published in 1999 and is likely a bit dated.
Other web programming languages and stacks have moved to an application server structure. With this approach, it is designed for hosting lightweight (rather than the heavy fork and exec) processes, often with a focus on a web facing interface for the applications.
Wikipedia lists a few C++ application servers:
- Tuxedo – Based on the ATMI standard, is one of the original application servers.
- Tntnet – Includes a template engine which allows embedding C++ code in HTML pages. Templates are compiled before run-time, and thus
very fast. Multi-threaded, supports object lifetime via scoped
variables.- CPPSERV – C++ servlet container.
- Wt – A web toolkit similar to Qt permitting GUI-application-like web development with
built-in Ajax abilities.- POCO C++ Libraries – A collection of open source class libraries including Poco.Net.HTTPServer.html
- CppCMS
The key point with this is you likely need to work within another framework. Writing a multithreaded scalable web application is beyond the means of most programmers.
Do realize that hosting may be an issue. Nearly every web host offers php, and many offer tomcat (java). You would have to do some digging to find one that offers one of the above application servers (very few offer cgi now days and even fewer would allow for random application just to run).
One of the advantages of languages that were coupled to an application server early in their development is that the structure of the application is consistent (java has the .war file which is the same no matter what container you deploy to – unless you’ve written container specific code). C++ containers appear to be incompatible between each other (I welcome any corrections on this matter).
3
What about developing your app functionality as a separate server and communicate with your UI using REST (or SOAP)? JS can process these messages and update UI. IMHO it is a better option.
Is this solution viable to you?
You can use tools like emscripten too, this converts LLVM bitcode into javascript. You can combine this with clang or clang++ to run your C or C++ in the browser. (Actually on the client side, unlike CGI)
(Unreal did their html5 port this way!)