Web Sequence Diagrams
Create sequence diagrams in seconds for free.
http://www.websequencediagrams.com

Why was this ad not blocked?
C++: A language for next generation web apps
Posted on: 2010-01-26 18:00:00
On Monday, I was pleased to be an uninvited speaker at Waterloo Devhouse, hosted in Postrank's magnificent office. After making some surreptitious alterations to their agile development wall, I gave a tongue-in-cheek talk on how C++ can fit in to a web application.

There were other cool presentations there too. Check it oot! Er, out!

During this presentation, I hope to convince you that the C++ programming language is ideal for developing your next web application.

You might be asking, Steve, Why C++? Why would I subject myself to this horrible language where you have to manage your own memory?

One reason is efficiency. Not only is C++ inherently fast. It also forces you to think differently. It discourages the use of overly complicated data structures that are built in to other languages. In C++, nesting more than one or two structures results in something that is simply too awkward to use. Instead, the you are forced to seriously consider using the simplest possible representation.

In addition, just about any library that you'd want to use has already been written, and has a free implementation available on the internet. Json, and CGI decoders are freely available. Also, as often overlooked, you have to handle UTF8 to Wide-character conversion, but this is easily achieved in a 10 line function.

The only thing you do have to be careful with is SQL database access. MySql is GPL'd, so you can't even link to its client library in a closed source app. SQLite seems to be free, except if you do business in Germany it is $1000 because they have a different definition of public domain.

Here is the first strategy that you might use to incorporate C++ into your web app. In this diagram, there are two things between the browser and your application -- the web server, and the php script. It is not as efficient as it could be, and there are also security implications with calling command line programs from php.

In this model, you write your C++ program as a CGI script directly, using one of the freely available query string parsing libraries. It is efficient and clean. When your browser requests information, the webserver starts your program, which spits out the response in json format. This response is then relayed back to the browser.

I use the above strategy for RhymeBrain. It includes some pretty hairy statistical algorithms that let it sound out any word that you put in. For example, you can enter the word "postrank" and see that it rhymes with such gems as "blank, prank, drank, tanked, and stank".

Because it's written in C++, I can run it on my super-powerful datacenter. It sits atop my sock drawer at RhymeBrain headquarters.

This powerful 1GHz beast can load the entire 260,000 word database and fire back the response in about 12 milliseconds. It does this from a cold start, for each request.

But there is a third strategy: If you write your own webserver, you can cut out the middleman and serve the request directly. Your javascript code makes a request, and all your web server has to do is call a function to send back the results.

Writing a webserver isn't that hard. Here is the complete implementation of the Hibachi web server. It supports virtual hosts, and perl and php scripting, among other things. It was written by former Waterloo-ite Anthony Howe, and won the 2004 International Obfuscated C Coding Competition.

Inspired by Hibachi, I wrote my own webserver and built WebSequenceDiagrams (which runs in a real data center..). Doing it this way reveals a new business model. It is possible to package up the entire web application into a single installer that runs on Windows and Linux. Since it's all integrated, there is no need for customers to fuss around with Apache or the numerous other moving parts that could break if I shipped separate components. (You can run it in your organization for as little as $99).

The only disadvantage is that the final executable is really small -- around 700K. It may be a little smaller than some customers are expecting. During the presentation, it was suggested that I ship it as an appliance, in a huge box, to compensate.

Conclusion

I hope that I've convinced you of the benefits of C++ in your next web application:
  • Reduced hardware costs
  • Readily available libraries for web tasks
  • Portability
  • Extreme flexibility in deployment

Want more programming tech talk?
Follow on Google Buzz
Subscribe to posts

Post comment

Real Name:
Your Email (Not displayed):
Text only. No HTML. If you write "http:" your message will be ignored.

Sebastian

2010-01-27 01:46:06
6 Years ago I was working on a CGI-Application written in C++ and just remembering it makes me wanna suck my thumb and weep.

meir

2010-01-27 02:02:04
why to write web server ?

why not just extend with c/c++ servers like nginx/lighttpd?

Michael

2010-01-27 03:48:50
You're lying, C++ applications are not portable!

Paul

2010-01-27 04:07:47
>Also, as often overlooked by anyone over 30, you

>have to handle UTF8 to Unicode conversion

Maybe anyone over 30 knows the difference between encoding and character set?

zproxy

2010-01-27 04:27:11
I guess if a compiler were able to succesfully tare apart the c++ application and translate parts of it to javascript it would be what you are after? :)

It could be easly done if the C++ was compiled to IL first.

Have a look at my jsc compiler at jsc.sf.net :) It will translate IL to javascript and more!

I do like the idea of a multitier application written as one application.

gpilotino

2010-01-27 04:31:29
portability should be the advantage of C++ over which language ? =)

Pim van Riezen

2010-01-27 05:26:07
I've been doing the same thing for years. Both the n2view monitoring frontend and the openpanel GUI are served through the Grace httpd classes, see grace.openpanel.com/wwg_http_server.html . Works like a charm and makes for a workable http service that can be installed without pain.

foo

2010-01-27 08:20:34
Why do still use php then?

Over30

2010-01-27 10:31:01
I'm well over 30 and I'm sure I've dealt more with the details of how Unicode works than you. I don't appreciate the crack.

fdara

2010-01-27 10:46:16
this is lol

JustAnotherJediCoder

2010-01-27 11:40:46
A few years ago I was working at a web/games company, and I had to write a backend logging server. I wrote it in my prefered language...C++!. It must've been about 10,000 lines of code, complete with a HTTP webserver service, a TCP/IP comms service between the logger and another C++ program which ran on another server. In short we're talking about 20,000 lines of code, which handled about 1GB of data per second, hardly touching the CPU load on the servers, allowing web browsers to check the status of the logger, ran for about 2 years after I had left the company (at which point I figure they had moved the servers), ZERO crashes, ZERO memory leaks, <1% CPU Load average.

The main bottlenecks were transfering the data from the logging applications' own memory into MySQL (since MySQL wasn't fast enough to handle 1GB/sec), and the TCP/IP connection between servers (I think we had a 1GBit/connection).

But don't get me wrong, Java is great for some things. For one without Java, the semiconductor industry would lose billions.

Java manages your memory by using more memory. Memory management is overrated. Most Java programs which do very little take up 100's of MB's, and leak, and when the garbage collector does work, it works hard (causes slow down).

In Java you may have a bug and the app still runs/leaks memory, in C if you have a bug it'll crash. I'd rather crash my car, and then take extra care on the roads, than have cancer and not no about it, until it's too late.

Java programs are slower too, which means you need more powerful CPU's. Java encourages a lazy way of coding and architecture, which will bite you in the a** in the long run.

C/++ code is portable ever heard of GCC, works on windows, linux, and just about everything? Tell me when someone makes a Java app for the iphone.

No offense to the wave of java kids out there. But there is a bigger difference between programmers who have an Assembly/C/C++ background and those that don't. Than there is between those who have a Java background and those who don't. Assembly/C/C++ foster a better, more critical way of thinking, than languages like Java, which teach you to hunt for the nearest library. "Oh my boss told me to make a First Person Shooter 3D game, where is the makeFPSGame(TYPE_3D); function and where is the Game API"

Young Padawan

2010-01-27 12:08:25
I am a Java coder but I subscribe to Jedi Coder's views.

Java gives you stuff to have a lazy stroll through a project but bloats things up. There's not much fun doing it.

Simple efficient code is the best solution in most of the cases. Machines love language that they can understand and that is machine code. Each level up from that adds more nonsense to what it is being said.

Java has its uses too.

old dude

2010-01-27 13:44:57
You neglected to mention the Apache DSO model... Dynamic Shared Objects. That's a huge omission.

Those who ignore the past are condemned to repeat it, BADLY.

berto

2010-01-27 15:31:52
interesting article and nice blog. have you looked into this? www.w3.org/2001/tag/doc/leastPower-2006-02-23

SomePunk

2010-01-27 19:10:06
Can't link to MySQL? Um we are talking about the internet right? If you aren't redistributing your application/selling it, you can use GPL code however you like.

Mike Borozdin

2010-01-28 11:33:13
>You're lying, C++ applications are not portable!

C++ application binaries are not portable, but source code is, if you are not stupid enough to use a lot of platform specific functions without trying to abstract them.

Joey Robert

2010-01-29 00:52:17
Your presentation was hilarious, hope to see you next month at Dev House!

Andrei Rinea

2010-02-03 03:35:51
Or... You can use a modern, compiled stack such as .NET (ASP.NET MVC for example) or Java (JSF/JSP) :P

DalSoft

2010-02-03 18:48:57
Seriously... for a useful web application your struggle to deliver anything useful in a reasonable amount in of time.

Your focusing on a slight performance gain (probably lost in browser to server latency anyhow) rather than productivity.

Lets take this blog for example, convert that into your next gen web app in C++, lets set a time limit say 24 hours. I don't think your get very far (compared to someone using a JIT'ed language) or you could prove me wrong of course :-)

Objective-C?

2010-02-03 18:59:34
"Tell me when someone makes a Java app for the iphone"

It's nothing to do with the language, it's because Apple only allow Objective-C on the iPhone, because they are closed a source corporate machine - there are plenty of Java apps running on other mobile devices.

foodforthought

2010-02-03 19:14:34
@DalSoft - you have a point, but would you rather pay a C++ coder for a bit longer to develop your web application or spend more on hardware for the life of the application to deal with inefficient processor usage and higher power consumption/cooling costs (in larger scale projects)?

This approach allows you to deploy your applications on simpler hardware (cheaper to replace as well as buy in the first place) with savings in almost every area and less downtime. Whats not to like?

Ronald Conco

2010-02-04 03:59:20
I actually did a similar implementation a couple of years ago, I had to build a flash based poker odds game for a casino, and the problem was that the php was not fast enough to run as odds calculator, so I had to build the odds calculator in C then call then call the executable from php which then send the results to the flash game. There was no other way around this....good read though....

nfma

2010-02-07 09:38:55
@foodforthought

It depends on what you want. If you want to go fast to market you might want to go with Rails or similar and replace it with faster implementations on a need basis...

Anyway, there are no "right recipes", you always trade things off.

julian

2010-02-23 04:05:32
Have you ever thought about security? I mean how many buffer overflow or format string bugs will be in there? Ok, if you are a C++ guru and write almost bugless code, you are fine. For the average programmer I can assure you that this will not be the case.

Chris Bruner

2010-03-05 04:21:15
You are right. I've got an application which is used on Linux systems all over North America for Life Insurance quotations. It was written in C++ because

1. It great grandparent was a dos program written in C++.

2. It uses a lot of secret special sauce that my company wouldn't want to sell as a php script.

3. It is blazingly fast.

4. The same base code runs on pocket computers PCs and webservers, because if (carefully) written without using templates, C++ is portable!.

Sangram

2010-03-14 01:25:09
Fully agree with post

love the customer expectation part

i had written c++ web app long back

NerdFight!

2010-03-14 13:15:11
Epic Nerd Rage Battle...

hobs

2010-03-29 08:47:08
>Can't link to MySQL? Um we are talking about the internet right? If you aren't redistributing your application/selling it, you can use GPL code however you like.

He is selling it.

L

2010-04-24 14:08:50
Python+cherrypy+sqlalchemy+mod_wsgi. Can be deployed on anything. It doesn't make sense to write the whole web app in C++ but it does make sense however to write some portions in C++ were you need performance.

Jay Godse

2010-05-05 14:03:40
I have some pages to help out with SQLite at:

www.squidoo.com/sqlitetutorial

www.squidoo.com/sqliteprogramming

www.squidoo.com/sqlitedatabaseprogramming

I also have videos on YouTube (just search for "sqlite tutorial" and look for my videos with the black command line screen.

Mohamad Farhat

2010-05-08 20:36:58
lololololololololololol.

man you made me laugh like never "The only disadvantage is that the final executable is really small".

I always had this problem, seriously! A friendly low-ranked client once told me : "what! you mean to tell me that we paid $60,000 dollars for for three cheap floppies!", are you out of your mind! give me five CDs and i dont care if you fill them with high resolutin pictures".

lolololololol.

Thanks for the superb article. mrfarhat@hotmail.com

Mohamad Farhat

2010-05-08 20:42:42
After reading other readers comments.

Sorry I forgot to say "LONG LIVE C++", and "THERE IS ONLY ONE C++" as in "There is only one Ronaldo".

lolol

hola

2010-05-11 10:34:35
wiiiiiiiiiiiii, im bored!!! what´s up??

mike

2010-07-04 13:36:24
Hello, welcome to 1994, you dang kids!

SciK

2010-08-04 08:30:34
“MySql is GPL'd, so you can't even link to its client library in a closed source app.”

GPL only forces you to release the source code if you *distribute* the program.

With a server-side app, you don't ;)

steve.hanov@gmail.com

Other posts by Steve

Compress your JSON with automatic type extraction JZBUILD - An Easy Javascript Build System Pssst! Want to stream your videos to your iPod? "This is stupid. Your program doesn't work," my wife told me Google Buzz gets less awful every day The simple and obvious way to walk through a graph Asking users for steps to reproduce bugs, and other dumb ideas Creating portable binaries on Linux Bending over: How to sell your software to large companies Regular Expression Matching can be Ugly and Slow C++: A language for next generation web apps qb.js: An implementation of QBASIC in Javascript (part 1) Zwibbler: A simple drawing program using Javascript and Canvas You don't need a project/solution to use the VC++ debugger Boring Date (comic) barcamp (comic) How IE <canvas> tag emulation works I didn't know you could mix and match (comic) Sign here (comic) It's a dirty job... (comic) Text-to-speech for domain names Pitching to VCs #2 (comic) Building a better rhyming dictionary Does Android team with eccentric geeks? (comic) Comment spam defeated at last Pitching to VCs (comic) How QBASIC almost got me killed Blame the extensions (comic) How to run a linux based home web server Microsoft's generosity knows no end for a year (comic) Using the Acer Aspire One as a web server When programmers design web sites (comic) Finding great ideas for your startup Game Theory, Salary Negotiation, and Programmers Coding tips they don't teach you in school When a reporter mangles your elevator pitch Test Driven Development without Tears Drawing Graphs with Physics Free up disk space in Ubuntu Keeping Abreast of Pornographic Research in Computer Science Exploiting perceptual colour difference for edge detection Experiment: Deleting a post from the Internet Is 2009 the year of Linux malware? Email Etiquette How a programmer reads your resume (comic) How wide should you make your web page? Usability Nightmare: Xfce Settings Manager Usability Nightmare: ktoon cairo blur image surface Automatically remove wordiness from your writing Why Perforce is more scalable than Git A complete blogging system in 1900 lines of php Optimizing Ubuntu to run from a USB key or SD card UMA Questions Answered Make Windows XP look like Ubuntu, with Spinning Cube Effect See sound without drugs Standby Preventer Stock Picking using Python Spoke.com scam Stackoverflow.com Copy a cairo surface to the windows clipboard Simulating freehand drawing with Cairo Free, Raw Stock Data Installing Ubuntu on the Via Artigo Why are all my lines fuzzy in cairo? Handling Unicode Form Data in PHP and Python A simple command line calculator Tool for Creating UML Sequence Diagrams Exploring sound with Wavelets A Fast Calorie Calculator for Windows UMA and free long distance UMA's dirty secrets Creating a Todo list in Ajax Installing the Latest Debian on an Ancient Laptop How to make the MSDN style tree view in Javascript Dissecting Adsense HTML/ Javascript/ CSS Pretty Printer Comment Spam Web Comic Aggregator Experiments in making money online How much cash do celebrities make? Draw waveforms and hear them Cell Phones on Airplanes Detecting C++ memory leaks What does your phone number spell? A Rhyming Engine Rules for Effective C++ Cell Phone Secrets