<?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>dimas priyanto- posts tagged with faye</title>
	<atom:link href="http://dimaspriyanto.com/tag/faye/feed/" rel="self" type="application/rss+xml" />
	<link>http://dimaspriyanto.com</link>
	<description>a guy with mouse and keyboard</description>
	<lastBuildDate>Fri, 08 Mar 2013 08:16:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Embedding Faye into Rails</title>
		<link>http://dimaspriyanto.com/2012/02/14/embedding-faye-into-rails/</link>
		<comments>http://dimaspriyanto.com/2012/02/14/embedding-faye-into-rails/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 05:30:34 +0000</pubDate>
		<dc:creator>dimaspriyanto</dc:creator>
				<category><![CDATA[Log]]></category>
		<category><![CDATA[in English]]></category>
		<category><![CDATA[faye]]></category>
		<category><![CDATA[faye-rails]]></category>
		<category><![CDATA[thin]]></category>

		<guid isPermaLink="false">http://dimaspriyanto.com/?p=588</guid>
		<description><![CDATA[So, realtime messaging is the main topic of this post, one of the option that i&#8217;m going to cover in this post is Faye. We..]]></description>
			<content:encoded><![CDATA[<p>So, realtime messaging is the main topic of this post, one of the option that i&#8217;m going to cover in this post is Faye. We can find a good tutorial by Ryan Bates on how to use Faye in Railscasts in <a href="http://railscasts.com/episodes/260-messaging-with-faye">this episode</a>. In there, we have a rails application as the main interface to the user and a service for the messaging in a separated service using Faye. That&#8217;s doesn&#8217;t fit my needs, I want to also do some processing with the messages to the database and other services.</p>
<p>To deal with this situation we will need to use <a href="https://github.com/jamesotron/faye-rails">faye-rails</a> created by <a href="https://github.com/jamesotron">James Horton</a>. We will need to run the application only with Thin, because typical rails rack-based server can&#8217;t handle open connection that needed by Faye and only Thin who can do this. First, add this to Gemfile:</p>
<pre>gem 'faye-rails'</pre>
<p>Run bundle install then add to config/routes.rb routing to the action we want, let&#8217;s say:</p>
<pre>faye_server '/stream', timeout: 25 do
end</pre>
<p>The routing rules above will set /stream path as the messaging resource, so make sure you&#8217;ve also add normal rails routing to the /stream path in the config/routes.rb, let&#8217;s say in home_controller and stream action and create the corresponding controller and with action (empty action is enough for now, we can create more advanced processing in here later).</p>
<p>We need to load the faye.js resource (this is the same name with faye routes we created before), we can do this by link the javascript like this in application template:</p>
<pre>javascript_link_tag 'http://localhost:3000/stream.js'</pre>
<p>Then create the view for the /stream path in the controller we created before, let&#8217;s say app/views/home/stream.html.erb, and put this javascript function to see the result of the messaging system:</p>
<pre>&lt;script type="text/javascript"&gt;
  $(function(){
    var faye = new Faye.Client('http://localhost:3000/stream');
    faye.subscribe('/stream/new', function(data){
      console.log(data);
    });
  });
&lt;/script&gt;</pre>
<p>We&#8217;ve owned everything we need so far, the javascript function above will subscribe to &#8216;/stream/new&#8217; events, each time new stream available it will log to the console the data being sent. So, let&#8217;s start our server using thin by run this command:</p>
<pre>rails s thin</pre>
<p>To see how is it going, let&#8217;s run a curl command that post a new stream message to the faye server:</p>
<pre>curl http://localhost:9292/stream -d 'message={"channel":"/stream/new", "data":"hello"}'</pre>
<p>And we should see on the browser console data &#8216;hello&#8217; we&#8217;ve sent before.</p>
]]></content:encoded>
			<wfw:commentRss>http://dimaspriyanto.com/2012/02/14/embedding-faye-into-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
