Kevin N. Wang

RSS
Mango sticky rice.. TOO good

Mango sticky rice.. TOO good

Farewell IBM

Just sent out my farewell letter to all my colleagues in IBM. It was a much harder exercise than I imagined and in fact in the end I became a bit sentimental. It IS true that I have wanted to move on for quite some time and that I am excited about the new opportunities. It is also true that this is an experience that gave me amazing learning, close friends and took me all over the world. I am actually feeling a bit sad.  It’s like shedding a layer that I have carried on for a long time - liberating yet stirring. Like the first of everything else, I am sure it will have its special place.

Thank you IBM. I am grateful for what you have given me and Farewell. 

Jun 2

The Snake Game with Faye

I spent some time over the weekend working on the muli-player Snake game, with a lot of javascript on the front end and a rack Faye server on the back end. It took me some time - quite a few things were not trivial.

First, the Faye server’s set up. Over at Railscast, Ryan Bates has an episode on “Messaging with Faye”, but I found it redundant to have faye running on its own server in addition to the web server. Faye can be used as middleware and it’s very easy to drop into an existing Rack app. In my case, I am using Sinatra, so the config.ru looks like this: 

To run the server, do 

rackup config.ru -s thin -E production

On the front end, Paul Jensen has some nice javascript code that I took as the base. I changed his code quite a bit, mostly refactoring it into models so I can expand it to a multi-user game. 

The game’s rules are pretty simple - the canvas refreshes every 1/10 of a second; the snakes keep traveling unless told to turn; when a snake eats food its body gets longer by 1; the game ends when a snake “eats” itself. 

Initially my thought was to have the server push out every single frame to the clients to ensure synchrony but it turned out to be too much data being passed around. I ended up with client communicating to the server only when its snake turns, eats food or dies, and sending over the “world” to update the other clients (again, for better synchrony). This works much better than the first approach and is also architecturally easier - there is no logic on the server side other than a broadcaster and clients handle their own states until being told to sync up with an incoming state. 

I deployed the game on Heroku - they use Thin as their server too it works. However, I have found that the latency is quite bad between browser clients and the server. It does not feel very real time. If I were to increase my Dynos or Workers the the messaging pipes would just break - I can see how it is so with Dynos - they are separate nodes after all, but I thought Workers are supposed to work off tasks on the same queue? 

To play with this game, clone my github repo and run it on local. The game is still very much unfinished, and there are definitely bugs. Submit patches or fork it as you please. 

The code is here

co-hacking this weekend?

I am going to do a hack + learn session this weekend to develop a Ruby event based / async server for the Snake game. I looked at several technologies in this space and decided to give Faye a try - Node based solutions are pretty cool too, and I’ll use another time to do a project on that.. 

What Faye really impresses me is that it’s rack based, so it is trivial to integrate it with Rails applications, and can easily leverage the other awesomeness in the Ruby ecosystem (the language, gems, hosting, etc). 

On the front end, I am going to use HTML5’s web socket and canvas to control the snake.. should be interesting. 

I’ll be doing this with some people together this weekend. if you want to join please tweet me @knwang - we’re all remote and planning just to use IRC to share and discuss. 

Some related reading: 

http://www.slideshare.net/igrigorik/event-driven-architecture-meshu-ilya-grigorik

http://railscasts.com/episodes/260-messaging-with-faye

http://faye.jcoglan.com/

http://www.slideshare.net/ismasan/websockets-and-ruby-eventmachine

http://diveintohtml5.org/

什么都可以问咩?王帅帅最爱谁腻?

当然最爱你啊

RailsConf Day 4

Keynotes of Day 4 were some of the best:  Check out Corey Haines keynote here on becoming awesome. and if you are a manager and claims that people are your best assets you need to check out how to optimize for developer’s happiness. Glenn Vanderburg gave an insightful presentation comparing software engineering with other engineering disciplines - are we craftsmen, engineers, architects, artisans all of the above, or none of the above? Watch it, it’ll be worth your time. 

Yehuda Katz / @wycats talked about “Building Rails Apps for the Rich Client”. I thought he was going to talk quite a bit on SproutCore but he barely touched it. I could not find the presentation for this so here is a summary: 

  • Rails can be very useful in making APIs for machine to machine communications. 
  • Q: If all I need is JSON, why do I need Rails?  Isn’t something like Sinatra better? 
  • A: Rails is not trivial, a lot is hidden. It’s a rich abstraction of HTTP. It takes care of a lot for you: sessions, cookies, security, standards, constraints, caching, etc. If you move away from Rails you have to do these yourself. and it’s hard to make things work well together. Rails does a lot of awesomeness underneath. 
  • Some rules on API design: 1) root has to have resource keys 2) no nested URLs. 3) nested object should follow 1) and 2)

Passing JSON back to the rich client to have a JavaScript templating engine (SproutCore, Backbone.js, Mustache.js etc) to render is a trend.. and Yehuda is making the point that Rails is still a good choice for building APIs. 

I picked up some nice tips from the “Bridging the Gap between Rails and JavaScript in Rich Client Apps” session (presentation here). A very brief summary - how to reuse code across Ruby and JavaScript in your app? 

  • Write JavaScript code in CommonJS modules
  • Use IncludeJS to use them in Ruby
  • DRY!

Matt Kelly / @mkelly12 gave the presentation on Backbone.js, a popular JavaScript templating engine. As I said before, having a MVC front end structure is a trend building rich client apps… and it can really do magic.. check out the Chop app that backbone guys just released (make sure to drag and drop on the code). The presentation itself is a lot of fun too!

FINAL WORDS: 

I got a lot from this conference - it has certainly been information overload, but I came back home excited about all the people doing interesting projects. Met many people who I have known but never got to meet in person. It is very true that open source is all about the community, and RoR has the best community there is.. it almost feels like a fraternity. 

RailsConf Day 3

Day 3: 

Dr. Nic  @drnic ‘s opened up the morning keynote to talk about Ruby’s history, but it’s really from Engine Yard to get folks to use JRuby and Rubinius.

Aaron Patterson / @tenderlove (only person in both Ruby core team and Rails core team) gave an "Intense" keynote - you have to see it to know what’s in it - it’s definitely more than you think. :-) He dived into some under the hood stuff in Rails 3.1 improvements  - prepared statement caching, new serialization strategies, streaming response, reduced middleware stack. 

The "Controlled Choas" session is a case study of NASA transitioning from legacy system (yes, Java is now considered Legacy now!!). The reasons behind the switch are 1) they want a easier interface (SOAP to REST) and 2) They started to lose developers because nobody wanted to work on old technologies. They switched to JRuby to make use of the JVM. The result: happy developers, team moving much faster, much more maintainable code base. 

The two BoF sessions I went to were awesome -

Ryan Bates / @rbates (of the Railscast fame) led a walkthrough on the Cancan gem 2.0 version (for role based authorizations). It’s still under development.. but is a huge step over 1.0. I have actually evaluated Cancan for one of my projects but found it somewhat limited on the finer level of controls. 2.0 would change that - I like where this project is going. 

Yehuda Katz gave a presentation on SproutCore. This is one of the things coming up that I am really excited to dig into (the other is the Ruby EventMachine). It has matured much from when I looked at it last time - for one.. there’s online documentation now! I think desktop like web applications are the way to go, and it’s really cool to have a framework like SproutCore to make things easier. There was a heated discussion on SproutCore vs Backbone.js and Yehuda’s message was: SproutCore has everything Backbone (mostly observers) has and more, and the argument that SC is too big is not so important in the bigger scheme of things. 

RailsConf - Day 2

My intention was to write one blog post per day chronicling my experiences but that turned out to be very difficult - sessions ran into late evenings (some BoF sessions ran as late as 10pm, but were really worthwhile), plus the casual chatters and the happy hours.. recovering from jetlag from Asia didn’t help either.. so didn’t get to write on it until today. 

But here it goes: 

Day 2:

@dhh gave the keynote in the morning to unveil Rails 3.1 RC - asset pipeline, jQuery default, native CoffeeScript + Sass support and all. No big architectural changes but solid steps to make Rails more organized. He also hinted that pjax and backbone.js could be there in Rails 3.2 (presentation layer MVC is a recurring topic this year).

Jeff Casimir / @j3 gave a very nice session on “Fat Models aren’t enough”, where he talked about better organization of code in the models, the presenter pattern (don’t just pass a bunch of instance variables into the view, pack them up in a presenter class), scopes (or the no need of ) with lots of code snippets. Presentation here

Another session that I found really good is “Confident Code” by Avdi Grimm / @avdi. Presentation here. He talked about the Nil objects being over used in Ruby is so true. The idea of killing all nil in the code is refreshing (therefore eliminating all the checking).  I’ll certainly incorporate that in my code.

Eric Ries, the godfather of the “lean startup” movement gave the evening keynote. I’ve seen him speak before, but it makes me think every time. If you are into building a software product start up, his work is a must read. Video is here

RailsConf Day 1 

Day 1 of RailsConf is the day of tutorials, which turned out to be quite good. 

I attended the HTML5 session by @subelsky in the morning and learned a bunch. Like everybody else, I have heard about the HTML5 buzz for a while, but never got deeper into it than scratching the surface. The session ran in the form of 13 labs, and drawing, sockets, native storage, and of course native media support are the highlights. In fac you can do many cools things with just html5 now.. such as this game. The presenter did a great job packaging the material as a series of easy to follow labs. You can get them here and follow it. It will all just take a couple of hours. 

I went to “Building Bulletproof Views" in the afternoon by the LivingSocial guys.. Solid presentation and I learned quite a lot.. I tend to agree with them that the rails view layer is wild west and is probably the least organized part of the framework, and I didn’t see much "best practices" before on this layer - good work that they put this together. Interestingly, I found half of the issues are made really easy with using an authorizing framework like Compass. They also talked about some tools like formastic, goose (a gem for tab navigations), responsive design and the mobile web. Presentation is here

Also, the Envy Labs guys gave sessions on Rails for Zombies (beginner’s course to Ruby on Rails) and Rails Best Practices. Check them out. 

Finally, IgniteRails is awesome! @drnic is hilarious