C++ on the back end of web app - c++

I have been searching the web for this information and I think I need some help with understanding this better.
I would like to learn how to write back-end of a web application in C++ and essentially how to output C++ to web pages and make it talk to a MySQL database. For the record I can write decent code in C++ but I never did it for web.
Wherever I go on the web and find people asking about this the first list of responses is WHY would you when yo have scripting languages. I am aware of the scripting languages, I have used them for years but I am running across cases where this is a requirement and I would like to learn more about it.
My intent it to write an app that uses Angularjs on the front and C++ on the back. I am a fairly well versed PHP developer and I might take this task on by writing PHP initially but I do need to account for a possibility of rewriting in C++ and this makes me think I should probably write it in C++ from the get go.
I understand that the most usual question is WHY I would use C++ instead of a scripting language so I will try and give a limited set of reasons. Please do understand I am not a pro in this aspect of C++ yet and I am evaluating the situation I am in.
So here are some of my reasons...
The project I am about to take on is bound to be very resource intensive and I would really like to gain all the speed I can possibly get from the get go. The more control I have over the app process the better. Essentially I need precision, I know and love C++ and it allows me to retain the control to a great degree..
The group of people I am talking to in the context of the project are aware of the advantages of C++ fro the resources and speed perspective and they hold a portion of the investment bag which gives them a vote in how the project will be approached. Time IS on my side, but i want to waste as little of it as possible.
I am comfortable with C++, have a C++ oriented mind and would love to use it in this context as much as possible.
So I guess my questions are...
Is there a good tutorial that can take me from the basics to
something intermediate?
How do I write a web site backend in C++?
How do I write C++ to work with MySQL on Linux.
Is there a way to do this on Linux servers?
(I believe Facebook it doing it).
I found this http://www.compileonline.com/compile_cpp_online.php on http://www.compileonline.com and it is giving me hope but I need more information to know how to get there.
I am aware that I am not asking an example code based question but I often find good reference posts on Stack that answer these types of questions so I hope someone can help me. I feel a bit lost here.

My preferred approach to building angular apps is to use the back-end pretty much strictly as a REST server.
Here are a few places that list options:
I'm searching a cgi lib in C to build a restful web service
and
How popular is C++ for making websites/web applications?
and
https://softwareengineering.stackexchange.com/questions/147445/how-does-one-interface-c-with-the-web-at-google-for-example
One option that seems to come up multiple times is http://cppcms.com/. Also http://www.webtoolkit.eu
Integrating angular will be pretty much the same as integrating with any other back-end. If you're using the back-end as a REST server you can pretty much statically serve all of the angular code.

Related

Will web development in c++ cgi really a huge performance gain?

I'm asking the question after reading this article
http://stevehanov.ca/blog/index.php?id=95
Also isn't it a penalty to use cgi instead of fastcgi ?
Update: why some people do pretend like in answer "that you get 20-30% performance improvement" ? Is it pure guess or is this number coming from solid benchmark ? I have looked at HipHop performance is more in the scale of 10 times.
I've done webdev in a few languages and frameworks, including python, php, and perl. I host them myself and my biggest sites get around 20k hits a day.
Any language and framework that has reasonable speed can be scaled up to take 20k hits a day just by throwing resources at it. Some take more resources than others. (Plone, Joomla. I'm looking at you).
My Witty sites (none in production yet) take a lot more (from memory around 5000% more) pounding (using seige) than for example my python sites. Ie. When I hit them as hard as I can with seige, the witty sites serve a lot more pages per second.
I know it's not a true general test though.
Other speed advantages that witty gives you:
Multi threading
If you deploy with the built in websrever (behind ha-proxy for example) and have your app be multi-threaded .. it'll load a lot less memory than say a perl or php app.
Generally with php and perl apps, you'll have Apache fire up a process for each incoming connection, and each process loads the whole php interpreter, all the code and variables and objects and what not. With heavy frameworks like Joomla and Wordpress (depending on the number of plugins), each process can get pretyy humungous on memory consumption.
With the Wt app, each session loads a WApplication instance (a C++ object) and it's whole tree of widgets and stuff. But the memory the code uses stays the same, no matter how many connections.
The inbuilt Web2.0 ness
Generally with traditional apps, they're still built around the old 'http request comes in' .. 'we serve a page' .. 'done' style of things. I know they are adding more and more AJAXy kind of thigns all the time.
With Wt, it defaults to using WebSockets where possible, to only update the part of the page that needs updating. It falls back to standard AJAX, then if that's not supported http requests. With the AJAX and WebSockets enabled clients, the same WApplication C++ object is continually used .. so no speed is lost in setting up a new session and all that.
In response to the 'C++ is too hard for webdev'
C++ does have a bit of a learning curve. In the mid nineties we did websites in Java j2ee. That was considered commercially viable back then, and was a super duper pain to develop in, but it did have a good advantage of encouraging good documentation and coding practices.
With scripting websites, it's easy to take shortcuts and not realize they're there. For example one 8 year old perl site I worked on had some code duplicated and nobody noticed. Each time it showed a list of products, it was running the same SQL query twice.
With a C++ site, I think it'd have less chance because, in the perl site, there wasn't that much programming structure (like functions), it was just perl and embedded html. In C++ you'd likely have methods with names and end up with a name clash.
Types
One time, there was a method that took an int identifier, later on we changed it to a uuid string. The Python code was great, we didn't think we needed to change it; it ran fine. However there was little line buried deep down that had a different effect when you passed it a string. Very hard to track down bug, corrupted the database. (Luckily only on dev and test machines).
C++ would have certainly complained a lot, and forced us to re-write the functions involved and not be lazy buggers.
With C++ and Java, the compiler errors and warns a lot of those sorts of mistakes for you.
I find unit testing is generally not as completely necessary with C++ apps (don't shoot me), compared to scripting language apps. This is due to the language enforcing a lot of stuff that you'd normally put in a unit test for say a python app.
Summary
From my experience so far .. Wt does take longer to develop stuff in than existing frameworks .. mainly because the existing frameworks have a lot more out of the box stuff there. However it is easier to make extremely customized apps in Wt than say Wordpress imho.
From people I've spoken with who've moved from PHP to Wt (a C++ web framework) reported significant improvements. From the small applications I've created using Wt to learn it, I've seen it run faster than the same PHP type applications I created. Take the information for what you will, but I'm sold.
This reminds me how 20-30 years ago people were putting Assembly vs C, and then 10-20 years ago C vs C++. Of course C++ will be faster than PHP/Rails but it'll take 5x more effort to build maintainable and scalable application.
The point is that you get 20-30% performance improvement while sacrificing your development resources. Would you rather have you app work 30% faster or have 1/2 of the features implemented?
Most web applications are network-bound instead of processor-bound. Writing your application in C++ instead of a higher-level language doesn't make much sense unless you're doing really heavy computation. Also, writing correct C++ programs is difficult. It will take longer to write the application and it is more likely that the program will fail in spectacular ways due to misused pointers, memory errors, undefined behavior, etc. In general, I would say it is not worth it.
Whenever you eliminate a layer of interpretive or OS abstraction, you are bound to get some performance gain. That being said, the language or technology itself does not automatically mean all your problems are solved. I've fixed C++ code that took many hours to process a relatively simple set of records. The problem was in the implementation, and the fix was not related to the language's features or limitations.
Assuming things are all implemented correctly, you're sure to get better performance. The problem will be in finding the bugs. One of the problems with C++ is that many developers are currently "trained" or accustomed to having a lot of details related to memory management behind objects. This eliminates the need to consider things like, "What can happen if I pass this pointer around to several threads?" Sometimes it works well, but not always. You still have some subtleties of the language that you need to consider regardless of how the objects hide the nasty details.
In my experience, you'll need several seasoned C++ developers watching over the code to be able to keep the bugs and memory leaks from getting out of hand.
I'm certainly not sold on this. If you want a performance gain over PHP why not use a Java (or better yet Scala) framework? These are much better for web development, have nice, relatively easy to use frameworks and avoid a lot of the headaches of C++. I've always seen one of the main pluses of web-development (and most modern non-scientific/high performance applications) as being able to avoid the headaches that come along with C/C++ development.

C++ Server-Side-Scripting

For once, I have come across a lot of stuff about the use of C++ being not advisable for SSS and recommending the use of so called interpreted languages like PERL and PHP for the same. But I need the advanced OO features and flexibility of C++ to ensure a scalable and more manageable code.
I have tried many internet articles and searches and none where helpful to the point that I still have no idea if it is possible to write SS-Scripts in C++ and if yes, then how.
I have thought of couple ideas, including writing a web-server in C++ and responding accordingly after parsing the HTTP request. But it would be re-inventing the wheel and I'll end up deviating from my main project and dedicating a lot of work to ensure a functional-cum-secure HTTP server.
I have also considered PHP extensions but again the approach also comes with its own baggage and overhead.
My questions are:
Is it possible to program SSS in C++?
If yes, then what are the approaches at my disposal.
Thanks!
Ignoring, for the moment, the advisability of using C++ for SSS, your first choice would probably be Wt. Contrary to the implications in some of the other answers, no development time is not likely to increase by 10x (or anywhere close to it). No, you're not missing all the nice infrastructure features you'd expect in things like PHP, Perl or Python either.
In fact, my own experience is rather the opposite: while PHP (for example) makes it pretty easy to get a web site up and running fairly quickly, producing a web site that's really stable, secure, and responsive is a whole different story. With Wt, rather the opposite seems to be the case (at least in my, admittedly limited, experience). Getting the initial site up and running will probably take a little longer -- but about as soon as it looks, acts, and feels the way you want, it's likely to need only rather minor tweaks to be ready for public use.
Getting back to the advisability question: developing in C++ may be a bit more complex than in some languages that are more common in the SSS market -- but it's still a piece of cake compared to doing security well. If somebody has even the slightest difficulty writing C++ (e.g., tracking and freeing memory when it's no longer needed), I definitely don't want them getting close to the code for my web site.
I wouldn't recommend it, but you can certainly write CGI scripts in C++ (or in C, or in FORTRAN). But why bother? Languages like PHP do a much better job more easily, and they seem to scale well for some pretty major sites.
CGI is the "standard" way to have C or C++ code handling web requests, but you might also look into writing a module that gets linked into the web server at runtime. Google for "apache module API" (if using Apache) or "IIS module" (if using IIS).
Can you afford 10x as much development time? All the infrastructure-ish bits that you take for granted in php, perl, python are non existent or much harder to use in C++.
I see only two valid reasons to do this:
1. You only have C++ on your platform.
2. The server really has very high performance needs that would benefit from problem specific optimizations.
You can write a CGI application in C++ using an appropriate framework (like this one). But I'd recommend just going with perl or php. It will save you much time. Those tools are just better suited for this kind of job.
EDIT: corrected the link
I couldn't understand your exact requirements (license, etc) but this might be what you are looking for http://cppcms.sourceforge.net.

Node.js or Erlang

I really like these tools when it comes to the concurrency level it can handle.
Erlang/OTP looks like much more stable solution but requires much more learning and a lot of diving into functional language paradigm. And it looks like Erlang/OTP makes it much better when it comes to multi-core CPUs (correct me if I am wrong).
But which should I choose? Which one is better in the short and long term perspective?
My goal is to learn a tool which makes scaling my Web projects under high load easier than traditional languages.
I would give Erlang a try. Even though it will be a steeper learning curve, you will get more out of it since you will be learning a functional programming language. Also, since Erlang is specifically designed to create reliable, highly concurrent systems, you will learn plenty about creating highly scalable services at the same time.
I can't speak for Erlang, but a few things that haven't been mentioned about node:
Node uses Google's V8 engine to actually compile javascript into machine code. So node is actually pretty fast. So that's on top of the speed benefits offered by event-driven programming and non-blocking io.
Node has a pretty active community. Hop onto their IRC group on freenode and you'll see what I mean
I've noticed the above comments push Erlang on the basis that it will be useful to learn a functional programming language. While I agree it's important to expand your skillset and get one of those under your belt, you shouldn't base a project on the fact that you want to learn a new programming style
On the other hand, Javascript is already in a paradigm you feel comfortable writing in! Plus it's javascript, so when you write client side code it will look and feel consistent.
node's community has already pumped out tons of modules! There are modules for redis, mongodb, couch, and what have you. Another good module to look into is Express (think Sinatra for node)
Check out the video on yahoo's blog by Ryan Dahl, the guy who actually wrote node. I think that will help give you a better idea where node is at, and where it's going.
Keep in mind that node still is in late development stages, and so has been undergoing quite a few changes—changes that have broke earlier code. However, supposedly it's at a point where you can expect the API not to change too much more. So if you're looking for something fun, I'd say node is a great choice.
I'm a long-time Erlang programmer, and this question prompted me to take a look at node.js. It looks pretty damn good.
It does appear that you need to spawn multiple processes to take advantage of multiple cores. I can't see anything about setting processor affinity though. You could use taskset on linux, but it probably should be parametrized and set in the program.
I also noticed that the platform support might be a little weaker. Specifically, it looks like you would need to run under Cygwin for Windows support.
Looks good though.
Edit
Node.js now has native support for Windows.
I'm looking at the same two alternatives you are, gotts, for multiple projects.
So far, the best razor I've come up with to decide between them for a given project is whether I need to use Javascript. One existing system I'm looking to migrate is already written in Javascript, so its next version is likely to be done in node.js. Other projects will be done in some Erlang web framework because there is no existing code base to migrate.
Another consideration is that Erlang scales well beyond just multiple cores, it can scale to a whole datacenter. I don't see a built-in mechanism in node.js that lets me send another JS process a message without caring which machine it is on, but that's built right into Erlang at the lowest levels. If your problem isn't big enough to need multiple machines or if it doesn't require multiple cooperating processes, this advantage isn't likely to matter, so you should ignore it.
Erlang is indeed a deep pool to dive into. I would suggest writing a standalone functional program first before you start building web apps. An even easier first step, since you seem comfortable with Javascript, is to try programming JS in a more functional style. If you use jQuery or Prototype, you've already started down this path. Try bouncing between pure functional programming in Erlang or one of its kin (Haskell, F#, Scala...) and functional JS.
Once you're comfortable with functional programming, seek out one of the many Erlang web frameworks; you probably shouldn't be writing your app directly to something low-level like inets at this late stage. Look at something like Nitrogen, for instance.
While I'd personally go for Erlang, I'll admit that I'm a little biased against JavaScript. My advice is that you evaluate few points:
Are you reusing existing code in either of those languages (both in terms of source code, and programmer experience!)
Do you need/want on-the-fly updates without stopping the application (This is where Erlang wins by default - its runtime was designed for that case, and OTP contains all the tools necessary)
How big is the expected traffic, in terms of separate, concurrent operations, not bandwidth?
How "parallel" are the operations you do for each request?
Erlang has really fine-tuned concurrency & network-transparent parallel distributed system. Depending on what exactly is the project, the availability of a mature implementation of such system might outweigh any issues regarding learning a new language. There are also two other languages that work on Erlang VM which you can use, the Ruby/Python-like Reia and Lisp-Flavored Erlang.
Yet another option is to use both, especially with Erlang being used as kind of "hub". I'm unsure if Node.js has Foreign Function Interface system, but if it has, Erlang has C library for external processes to interface with the system just like any other Erlang process.
It looks like Erlang performs better for deployment in a relatively low-end server (512MB 4-core 2.4GHz AMD VM). This is from SyncPad's experience of comparing Erlang vs Node.js implementations of their virtual whiteboard server application.
There is one more language on the same VM that erlang is -> Elixir
It's a very interesting alternative to Erlang, check this one out.
Also it has a fast-growing web framework based on it-> Phoenix Framework
whatsapp could never achieve the level of scalability and reliability without erlang https://www.youtube.com/watch?v=c12cYAUTXXs
I will Prefer Erlang over Node.
If you want concurrency, Node can be substituted by Erlang or Golang because of their light weight processes.
Erlang is not easy to learn so requires a lot of effort but its community is active so can get help from that, this is only the reason why people prefer Node .

Experiences using Wt C++ framework? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Has anyone seriously used Wt?
Did it work well?
Did you experience certain limitations? Or advantages?
Wt is a C++ library for developing web applications.
Please avoid the discussion of whether C++ is a good language for web development. I just want to give Wt a try because it seems like it could be a fun thing to do.
I've been using Wt to build apps that directly link to C++ libraries but don't particularly make a lot of effort to exercise the layout features such as CSS. So far, it works great as a replacement for GTK when building these kind of applications. I'm a Linux user exclusively and I'm also one of the unwashed heathen who actually enjoys programming in C++, and this framework is a perfect way for me to build an application that can actually be used across many platforms.
I have not personally used the framework, but have discussed it with a few people that have. They didn't really have any limitations, but I found it hard to believe they were compiling every time. Their main comment was that it was quite a light load on the server in terms of memory usage. Personally, I think the interpreted languages of php, python, ruby, etc work well with the nature of web development - but that's not the question you asked. Probably the biggest advantage is being able to use your existing skill set to work in a new medium.
There are also a few good comments online discussing pros and cons. Here is one I found http://discuss.joelonsoftware.com/default.asp?biz.5.599655.33
However, I think the main answer here is that without a specific project requirement in mind, it is going to be difficult to evaluate any framework for suitability. If you think it will be fun to try coding a few things with it, then give it a go. That is going to be the best (if not only) way to determine if it suits your needs.
I have tried several C++ embedded web servers. They tend to be a challenge to use, and not Windows friendly.
( You do not mention your platform. If you are on Unix, then I suspect you will find the available servers easier to use, and can probably ignore this answer. If you are on Windows, read on ... )
I have tried Wt, but was defeated by the massive installation, which takes hours to compile and generates page after page of warnings, and the extensive learning curve. Wt is modeled on Qt, so if you are familiar with Qt, the learning curve will be much less of a challenge.
I have tried Webio by John Bartas I liked the concept and it worked well. However, I found it overly complicated to use and the server code hard to understand. A lot of the complexity of Webio is caused by using an “HTML compiler” to hide the HTML pages that control the appearance of the GUI inside a file system embedded inside the application code. I prefer to have the HTML pages outside in plain view where I can adjust the GUI without recompiling the application.
I have also looked at TWS This is by Richard Hipp who is responsible for SQLITE and FOSSIL and of whom I am a great fan. However, TWS has not been maintained since 2001 and is not really WINDOWS, so I reluctantly decided not to pursue it.
In the end I rolled my own, called WEBEM based on a minimally modified version of the boost::asio web server. In concept similar to, but simpler than TWS, it permits html code to execute C++ methods.
To be honest, I had looked on it but I see one significant Wt design flaw -- it modeled after Qt. Trying to make web applications to look and behave like GUI.
I think, this approach is wrong. There should be clear separation between client side and server side.
If you are interested in C++ web programming take a look on CppCMS which has more traditional MVC model.
Note, I have biased opinion, because I'm developer of CppCMS.
Made a todo list app using it. works great, no problems.
I currently use it to develop a GPS measurements processing web application, based on processing algorithms implemented in C. It works well and has a good synergy with legacy C/C++ code.
It's documentation its not so extensive and the lack of learning tutorials and related books makes it somehow hard to learn.

Are there any web frameworks for compiled languages like C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
On our embedded device, we currently use PHP for its web interface, and unfortunately it's quite slow. We've been experimenting with Python, but is seems (at least on FPU-less ARM architecture) to be as slow as PHP.
Therefore we're thinking about implementing web interface in some compiled language like C++, but so far the only thing we've found is Wt, which looks more like desktop than web framework and it's documentation is rather complicated for a beginner.
So my question is: do you know about any good web frameworks for C/C++? What would make me totally happy would be something like C++ Django, but I doubt such thing exists :-)
What is the reason your site is slow in PHP? Have you profiled the code to figure out the bottle necks? Replacing the entire codebase with another language is likely to be too large a tool for the job.
Perhaps if some portion is computationally intensive, then maybe you can implement a PHP module in C++ to do the job.
If I were you, I would give Wt a try. I don't think you will find another solution as complete and easy to use as Wt with similar performance. The mailinglist are active, and has regular posts of people who use it on embedded devices. The Wiki (here) of the project mentions some numbers for embedded deployment and performance on ARM.
You can take a look at CppCMS. I don't know how mature or good it is, but it sounds like what you are looking for.
Another option is to write the main application in Python/PHP and then rewrite the critical parts in C, which seems more sane to me.
I had just seen this link as a referrer to CppCMS. Several questions/answers about it.
Q. How is it mature?
A. There is a Wiki 100% based on it. Developers blog runs on CppCMS as well. So you can see it quite works.
well. At least for author.
Q. "What would make me totally happy would be something like C++ Django"
A. Actually it is "C++ Django". Many Ideas had been taken directly from Django, like
forms, template inheritance and some more. So you would probably feel at home
Q. Nanogear
A. I had taken a look on it... It has many plans but not-too-much code.
Q. How many projects using it.
A. Not many. It is quite new project.
Q. Who am I?
A. To be honest I'm the developer of CppCMS.
Do you really need a web framework or just an html templating library?
See here for template options: C++ HTML template framework, templatizing library, HTML generator library
One thing to look at here is using C++ for some sort of web service instead of an actual web app development environment. On one of my current projects we generate JSON(and parse for requests) then let the client(web browser) deal with the rest. You have a great deal of computing power available to you on the client machine, mix in some javascripty magic and boom, now your embedded device can go back to doing what its supposed to do. I personally kind of like Wt and think it is your best bet if you want to dev the whole "web app" thing in C++.
As to the specifics of how you get your C++ code to start serving up web services, you can use the amazing boost.asio library(they have an http server example) and json_spirit(I think its on codeproject)
How about Nanogear. I haven't used it, so I'm not sure if it is a good fit. Here is a quote from the website.
C++ is a great, general purpose
programming language but no modern,
easy-to-use and powerful C++ web
frameworks were made during these
years. We try to fill the gap with
Nanogear. Inspired by the Zend
Framework and Rails we want to bring
C++ to the web, easily.
Here is a link to the older googlecode site Google Code Nanogear Looks like there is more info there.
Try Lua. There is Xavante embeded web server somewhere on LuaForge.
It's very fast, very small and very extensible - as all about lua is.
You have power to use Lua where scripting fits betteer, and use C++ functions where in need of more speed and conection to existing native code.
Since no one else has said it, I will. Try Java. Java is a compiled language with a good web framework. It is also very scalable. Every coperation I have worked for uses java as its main web-technology, and this is mainly due to this scalability factor. Not sure if it will work with your architecture, but it answers the question:
Are there any web frameworks for compiled languages like C++?
You may give a try to embedded version of CppCMS.
It's quite new and exprerimental development but It looks like it is
much more suitable then Wt because it is ore web oriented (more closer to Django)
and not GUI like.
Check out the ffead c++ application framework (http://code.google.com/p/ffead-cpp/), its developed on the lines of Spring for JAVA.
You can have a look at Tntnet. It uses its own template language which looks a bit like PHP, only with much more additional tags.