<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stephen Belanger &#187; node.js</title>
	<atom:link href="http://stephenbelanger.com/tag/node-js/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenbelanger.com</link>
	<description>Just another coder making the web a better place.</description>
	<lastBuildDate>Fri, 18 Nov 2011 20:46:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to make Socket.IO work behind nginx (mostly)</title>
		<link>http://stephenbelanger.com/2011/09/21/how-to-make-socket-io-work-behind-nginx-mostly/</link>
		<comments>http://stephenbelanger.com/2011/09/21/how-to-make-socket-io-work-behind-nginx-mostly/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 17:09:57 +0000</pubDate>
		<dc:creator>Qard</dc:creator>
				<category><![CDATA[node.js]]></category>
		<category><![CDATA[socket.io]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://stephenbelanger.com/?p=186</guid>
		<description><![CDATA[Most web hosts with node.js support host it behind an nginx proxy. Sadly, Socket.IO doesn't work at all behind nginx without a bit of hacking.]]></description>
			<content:encoded><![CDATA[<p>UPDATE: News from <a href="http://github.com/jpetazzo">jpetazzo</a>:</p>
<blockquote><p>dotCloud now has beta websockets support, so this hack should no longer be necessary. Just point your custom domain to experimental-gateway-1.dotcloud.com instead of gateway.dotcloud.com and you will be using a websockets-aware load balancer instead of the default one running Nginx.</p></blockquote>
<p>Most web hosts with node.js support host it behind an nginx proxy. Sadly, Socket.IO doesn&#8217;t work at all behind nginx without a bit of hacking. Currently there&#8217;s no vhost-supported way to run websockets through nginx, but we can at least get the xhr transport to work properly&#8211;basically everything can do xhr-polling.</p>
<p>Turns out that nginx doesn&#8217;t really like how Socket.IO uses the &#8220;Connection: keep-alive&#8221; header, so lets just remove that. All we need to do is overwrite a function in the xhr-polling transport. This should do it;</p>
<pre name="code" class="javascript">
io.configure(function() {
  io.set("transports", ["xhr-polling"]);
  io.set("polling duration", 10);

  var path = require('path');
  var HTTPPolling = require(path.join(
    path.dirname(require.resolve('socket.io')),'lib', 'transports','http-polling')
  );
  var XHRPolling = require(path.join(
    path.dirname(require.resolve('socket.io')),'lib','transports','xhr-polling')
  );

  XHRPolling.prototype.doWrite = function(data) {
    HTTPPolling.prototype.doWrite.call(this);

    var headers = {
      'Content-Type': 'text/plain; charset=UTF-8',
      'Content-Length': (data &#038;&#038; Buffer.byteLength(data)) || 0
    };

    if (this.req.headers.origin) {
      headers['Access-Control-Allow-Origin'] = '*';
      if (this.req.headers.cookie) {
        headers['Access-Control-Allow-Credentials'] = 'true';
      }
    }

    this.response.writeHead(200, headers);
    this.response.write(data);
    this.log.debug(this.name + ' writing', data);
  };
});
</pre>
<img src="http://stephenbelanger.com/?ak_action=api_record_view&id=186&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://stephenbelanger.com/2011/09/21/how-to-make-socket-io-work-behind-nginx-mostly/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Finally open-sourced some new stuff</title>
		<link>http://stephenbelanger.com/2011/06/13/finally-open-sourced-some-new-stuff/</link>
		<comments>http://stephenbelanger.com/2011/06/13/finally-open-sourced-some-new-stuff/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 02:36:11 +0000</pubDate>
		<dc:creator>Qard</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery.droploader]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[socket.io]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[real-time]]></category>
		<category><![CDATA[s3]]></category>
		<category><![CDATA[xhr]]></category>

		<guid isPermaLink="false">http://stephenbelanger.com/2011/06/13/finally-open-sourced-some-new-stuff/</guid>
		<description><![CDATA[Express-asset, Jquery-droploader and Chattan--three awesome new projects I just open-sourced.]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/qard/express-asset">Express-asset</a></p>
<p>A handy middleware utility for node.js and express to queue scripts and styles to be rendered later. Supports supplying javascript via anonymous functions to avoid multi-line string issues and also has built-in minification, which can be enabled when you render the scripts.</p>
<p><a href="http://github.com/qard/chattan">Chattan</a></p>
<p>This is the expanded source of the chat demo I made for my Node.js presentation at OKDG. I added CouchDB user persistence, S3-backed file uploading and also inline media embedding via another open-source project of mine called <a href="http://github.com/qard/embedify">Embedify</a>.</p>
<p><a href="http://github.com/qard/jquery-droploader">Jquery-droploader</a></p>
<p>A jquery plugin for xhr-based upload management with support for drag-and-drop regions. I used this for handling file uploads to S3 in Chattan.</p>
<img src="http://stephenbelanger.com/?ak_action=api_record_view&id=180&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://stephenbelanger.com/2011/06/13/finally-open-sourced-some-new-stuff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s chat about node.js</title>
		<link>http://stephenbelanger.com/2010/12/01/lets-chat-about-nodejs/</link>
		<comments>http://stephenbelanger.com/2010/12/01/lets-chat-about-nodejs/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 19:44:27 +0000</pubDate>
		<dc:creator>Qard</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[socket.io]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[express]]></category>
		<category><![CDATA[expressjs]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[okdg]]></category>

		<guid isPermaLink="false">http://stephenbelanger.com/?p=117</guid>
		<description><![CDATA[Last night I had the pleasure of presenting on <a href="http://nodejs.org">node.js</a> at the November <a href="http://okdg.org">OKDG</a> meet. It was a lot of fun, and there seemed to be quite a bit of interest in the technology. Well, now I've got the slideshow available in PDF form for all to see. :)

Not only that, the chat demo created in the presentation will remain live for all to play with. In addition to what was covered in the presentation, it also has a basic message filtering system to convert URLs to embedded content. Try posting a URL to an image, Youtube video or PDF and you can see it in action. <a href="http://chat.stephenbelanger.com">Check it out!</a>]]></description>
			<content:encoded><![CDATA[<p>Last night I had the pleasure of presenting on <a href="http://nodejs.org">node.js</a> and <a href="http://socket.io">socket.io</a> at the November <a href="http://okdg.org">OKDG</a> meet. It was a lot of fun, and there seemed to be quite a bit of interest in the technology. Well, now I&#8217;ve got the slideshow available in PDF form for all to see. <img src='http://stephenbelanger.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Not only that, the chat demo created in the presentation will remain live for all to play with. In addition to what was covered in the presentation, it also has a basic message filtering system to convert URLs to embedded content. Try posting a URL to an image, Youtube video or PDF and you can see it in action. <a href="http://chat.stephenbelanger.com">Check it out!</a></p>
<p><iframe src="http://docs.google.com/gview?url=http://stephenbelanger.com/wp-content/uploads/2010/12/Let__s_chat_about_node_js1.pdf&amp;embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe><a href='http://stephenbelanger.com/wp-content/uploads/2010/12/Let__s_chat_about_node_js1.pdf'>Download as PDF</a></p>
<img src="http://stephenbelanger.com/?ak_action=api_record_view&id=117&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://stephenbelanger.com/2010/12/01/lets-chat-about-nodejs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Node.js &#8211; I&#8217;m a fanboy already.</title>
		<link>http://stephenbelanger.com/2010/02/17/node-js-im-a-fanboy-already/</link>
		<comments>http://stephenbelanger.com/2010/02/17/node-js-im-a-fanboy-already/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 03:31:46 +0000</pubDate>
		<dc:creator>Qard</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[narwhal]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[v8]]></category>
		<category><![CDATA[v8cgi]]></category>
		<category><![CDATA[web server]]></category>

		<guid isPermaLink="false">http://www.stephenbelanger.com/2010/02/17/node-js-im-a-fanboy-already/</guid>
		<description><![CDATA[About a week ago I discovered a unique new programming technology called <a href="http://nodejs.org/">node.js</a>. It's an event-driven javascript system running on Google's V8 engine and is primarily focused on functioning as a web server. I hesitate to call it a web server however--it's much more than that!]]></description>
			<content:encoded><![CDATA[<p>About a week ago I discovered a unique new programming technology called <a href="http://nodejs.org/">node.js</a>. It&#8217;s an event-driven javascript system running on Google&#8217;s V8 engine and is primarily focused on functioning as a web server. I hesitate to call it a web server however&#8211;it&#8217;s much more than that!</p>
<p>Using this amazing new system I was able to integrate support for the new WebSockets specification that Google recently added support for in Chrome, and I used this to make a simple real-time chat application with emoticon support and translation of image links to inline images and youtube links to inline videos.</p>
<p>&#8230;and the best part? This only took me about an hour!</p>
<p>There are some simple architectural differences that make it vastly more powerful that most any other popular web programming languages. One of my favorite characteristics of it&#8217;s unique design is that variables assigned in a global scope can be accessed across all requests&#8230;that means regular database querying becomes completely unnecessary! You could create a json object where you store your data, then check it over every once in awhile, and apply any changes to the database. It&#8217;d work as a query queue of sorts that would greatly reduce database interaction overhead and it wouldn&#8217;t require setting up some other incredibly complex software to proxy the requests!</p>
<p>I&#8217;m also quite fond of the branching design. There is a module called &#8216;http&#8217; for running web servers, but it&#8217;s really just routing everything through the tcp module, which essentially does the same thing. The &#8216;http&#8217; module just simplifies the interface and makes common actions like sending headers more elegant. It&#8217;s a clever design really; supply varying levels of complexity in control to match varying levels of complexity in application&#8211;not everyone is going to want the ability to create a mail server, so why would they want to have to access tcp directly? Inversely; not everyone is only going to want a web server, so why would you only supply access to http-related code?</p>
<p>Have a look at this sample code I ripped-off from the homepage of node.js;</p>
<pre>
var sys = require('sys'),
   http = require('http');
http.createServer(function (req, res) {
  setTimeout(function () {
    res.sendHeader(200, {'Content-Type': 'text/plain'});
    res.sendBody('Hello World');
    res.finish();
  }, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');
</pre>
<p>Isn&#8217;t it so simple? You just include a few modules, run http.createServer() with a callback function passing in the request and response objects and tell the server to listen on whatever port you want and whatever domain you want. In the callback you can branch of by breaking apart request.url to find the user&#8217;s query and use that to redirect via a switch block or whatever other means you see fit. Best of all, it&#8217;s asynchronous, so you don&#8217;t need to halt execution for every little thing. It&#8217;s even compatible with many common Javascript utility libraries such as <a href="http://documentcloud.github.com/underscore/">Underscore</a></p>
<img src="http://stephenbelanger.com/?ak_action=api_record_view&id=83&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://stephenbelanger.com/2010/02/17/node-js-im-a-fanboy-already/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

