When SPDY was first announced I didn’t pay too much attention as history has shown it takes years before these things become widely adopted, if ever. Recently though, Twitter launched support for the protocol and I noticed (via this Chrome plugin) that quite a few of the sites I visit on a regular basis (mostly Google properties) are actually using SPDY in production.
Interesting. I guess it’s time to pay attention.
But, WebSockets!
My first question, without knowing too much about SPDY, was what its relation was to WebSockets — are they two competing implementations solving the same problem? The short of it is: No, they are complimentary and will coexist. Here’s my best attempt to relate the two:
SPDY is an augmentation to HTTP with the goal of making synchronous HTTP requests faster.
WebSockets is an alternative to HTTP with the goal of facilitating real time communication.
The big point here being that SPDY shouldn’t require too much application-level refactoring, whereas supporting WebSockets means building an application specifically for bi-directional communication.
Push
A word often bandied about is push and it has two different meanings with these technologies.
SPDY Push is a technique where more than just the requested resource is sent down the pipe to a browser, but within the context of a single request. Example: on a given HTML page there’s a good assumption that the corresponding .js and .css and a few images will be requested. SPDY can send all of these in a single request, eliminating quite a bit of RTT (round trip time).
WebSockets Push facilitates asynchronous communication between the server and browser. The server receives new data from another source and rather than waiting for the browser to request the new data, it is simply pushed through an already-open connection.
Complimentary
The two protocols are actually complimentary. WebSockets makes its initial handshake with servers over HTTP to discover if the ws:// protocol is supported, and one of SPDY’s primary methods of optimization is compressing and caching HTTP request headers.
Header compression resulted in an ~88% reduction in the size of request headers and an ~85% reduction in the size of response headers… We found a reduction of 45 – 1142 ms in page load time simply due to header compression.
So, in an ideal future the RESTful request-based web is driven by SPDY, and all of the real-time communication and “app-ifying” is handled via WebSockets. Two peas in a pod!
Hi, nice write up. You have helped me understand the difference between these 2 seemingly similar techs. I think that SPDY should find a new term besides Push as it adds confusion and does not describe the act of sending multiple resources simultaneously.
Forgecraft is a game currently in development using Ruby on Rails, Backbone.js, and all sorts of HTML5 buzzwords. Read an introduction here, and play the demo here.
With Forgecraft, and any game really, there are usually quite a few animations running concurrently. I found these moments to be choppy, unresponsive and frustrating when they were implemented in javascript (especially on lower CPU environments like the iPad) and needed another solution. CSS3 transitions were an obvious choice to check out, as they’ve been shown to be a great way to add a little pizzazz to a modern web app and can be executed natively (and even GPU accelerated).
But, can they provide the technical underpinnings for an interactive gamely element? To be effective in this context, they’d have to be:
responsive, consistent and reliable
noticeably smoother than their javascript counterparts
simple to integrate with scripting
Regarding the first 2 points, CSS3 transitions’ effectiveness will really depend on your particular use-case. With Forgecraft they were definitely response, reliable, and smoother than javascript and I decided early on to use them in lieu of javascript wherever I could. But, I won’t go into the benchmarks and A/B comparisons in this article… perhaps later. Let’s just skip straight to the useful part:
Integrating CSS3 transitions with the game’s scripting is pretty straight forward:
Define transitions in CSS on your master element
Define classes with resulting properties changed
Trigger a class change using javascript
Listen for the transition-end event with javascript
Of course, there are a few pitfalls with each of these steps that I’ll go into here. Let’s take an example from Forgecraft: the Bonus Strike event. Randomly while forging the player will see a bar pop-up like this:
The bar moves from left-to-right and the player’s goal is to click the icon when the moving bar is directly under the target (large white) bar. The moving bar is animated with CSS3 transitions.
Defining the CSS:
We have a bar that needs an initial negative offset (the start position) that transitions to its final position. Simple enough: we define the base CSS on the #bar element and give it two classes (.new and .activated) defining each of its states. We also define the transition between the two states:
One pitfall you may run into: for the animations to work in Firefox, you have to explicitly define the initial state for whatever property you are transitioning. In this example we are transitioning the left property of the #bar element. You normally wouldn’t define left: 0; — that’s the default! But Firefox requires this to trigger the transition when that property changes.
Triggering the Animation in Javascript
Using jQuery, we simply apply the classes that we defined in the CSS when we want to trigger the transition.
Listening for the transition-end event
CSS3 Transitions would be useless if we couldn’t get their context from within our scripting. Luckily, a series of events are fired in javascript while the transitions are running, just like you’d expect in a javascript-based animation. The primary event we care about is when the transition ends as you’ll most likely want to trigger callbacks.
Unfortunately, each browser vendor has decided to name these events differently… typical huh. Modernizr to the rescue! (If you’re not using Modernizr, you should be. But that’s another blog post altogether).
There’s a hidden gem in the comments in the source code of Modernizr that explains how to use its .prefixed() API to make a simple wrapper around the browser-specific transition event names. Here’s how I implemented it for the transition-end event. Feel free to use this wherever you need it:
Bam, now we can use one simple event binding to handle all browsers:
Stopping a Transition Early
When the player hits the hammer-and-anvil icon during the Bonus Strike event, I needed to stop the animation early. The easiest way I found to do this, was to just set the animating property to its current value. The transition delta becomes 0, so the animation stops:
Transition: End
You may find that integrating CSS3 Transitions into your game makes your interactions smoother and more responsive, and with a simple API for scripting against transition events, developing against them should look and feel a lot like working in pure javascript.
Forgecraft is a game currently in development using Ruby on Rails, Backbone.js, and all sorts of HTML5 buzzwords. Read an introduction here, and play the demo here.
One of my primary goals while building Forgecraft is to learn as many new technologies as possible. I’ve learned the hard way that it’s better to leave this kind of exploration in your hobby projects and out of your production “for real” stuff. And, seeing as how there are so many emergent technologies right now in web development there’s a lot to explore!
Whole Hog is Too Much Hog
MongoDB has quite a bit of steam behind it in the Rails/Ruby community (and plenty of other places too), and with projects like Mongoid and MongoMapper (I went with Mongoid) it’s an easy drop-in replacement for Active Record that maintains all those ORM conventions you know and love. I decided against the replacement approach for one primary reason: I wanted to use existing, robust, and actively developed libraries that rely on Active Record.
Example: There’s no reason to roll your own authentication/user system when there are insanely popular and feature-full libraries already in place for them like Devise and authlogic (I went with Devise).
Fortunately combining Active Record with Mongoid and getting the best of both worlds proved to be easy and painless.
The Set Up
The instructions for Mongoid include a step to remove the Active Record libraries from being loaded and delete your database.yml. Simply skip that step and both systems will run in conjunction.
One small operational change you’ll have to make during development is explicitly defining when your rails generators should use Active Record. If you want to make an AR-driven model, your generators will look like:
rails g active_record:model Player … produces a model the extends from ActiveRecord::Base.
And by default, the data generators will use Mongoid:
rails g model Skill … produces a model that includes Mongoid::Document.
The Implementation
Here’s how this combo system really shines in my opinion: get all the great features of gems built on AR with all the schema less features of Mongo. I’ll walk through an example:
In Forgecraft, the authenticating object is the Player and players have many Skills which, right now, are Accuracy, Craftsmanship, and Perception. But, the skill list will likely grow and change over time as the game evolves. A typical AR/SQL based approach would be to create a skills table and a join table between players and skills resulting in a complex multi-table query to get a player’s complete skills.
It’s clear here that the Player and Skills would fit well together as a single document in Mongo, queried all at once in one tidy object. But, since the Player is also our authentication model, using Mongo would prevent us from using Devise to handle all of that boring and complex auth stuff for us.
Enter Hybridization. Forgecraft’s implementation uses the typical Devise set up around the Player object (again all on top of AR). All of a player’s skills go into a single document with a reference to the player, like so:
To set up the relationships like one would expect with Active Record is a simple method on your Player object:
Bam! Now we can retrieve our skills as a single document (fast), and add new skills with ease (easy). Since Mongo is schema less, you could even have a different set of skills per player. Again, all of this is achievable through SQL — it’s been done for decades — but it just makes more conceptual sense to treat these as a single document.
Querying and working with these objects and their relationships looks and feels just like active record:
I know, I know — any time you introduce an additional system, especially a datastore, you introduce a lot of complexity, failure modes, testing, etc. But, I honestly think the benefits are worth this extra complexity. Speed, ease of development, and the potential for less schema-driven growth make this an ideal environment for me. I highly recommend it, and will be using it in the future!
The fuzzy finder in SublimeText2 is great, but if you’ve added a rails folder to your project you may see a bunch of cruft in the result list most likely due to cache files. Here’s a generic rails project file I use that keeps the fuzzy finder pretty clean.
Note: this is a Rails 3.0 project with jammit and compass installed. The exclusion of the assets folder is probably not ideal in a Rails 3.1 project.
The New Relic API is pretty straight forward with regards to listing Accounts and Applications, but I personally don’t have this use-case. So, please let me know if the current implementation is lacking any functionality for those of you that do!
New Relic has quickly become an invaluable tool for monitoring performance of Key Ring for me. I found myself constantly checking the dashboard, partly for the graph, but mostly for the quick metrics at the top. Namely, I pay the most attention to requests-per-minute, response time, and errors.
Why keep a browser window dedicated to this? Fortunately, New Relic provides an API, so I whipped together a quick and dirty menu bars app to display the stuff I care about. Here’s what it looks like:
If you’d find this useful, feel free to download the app here.
Warning: The app is very simple. It prompts you for your New Relic API key and then loads your first Account and first Application. Perfect for my needs, but I’m open to suggestions. Do a lot of you have multiple accounts or applications? If so, I can add an account/application selector.
Update: I’ve added support for multiple accounts and applications. Get the new version here!
Feel free to contact me a brit at britg dot com with suggestions or feature requests!
Thanks! I’m planning to add a few more features and put it on the Mac App Store. If that doesn’t work out, I’ll share it on github for sure. In the mean time, you can check out this older project that’s open source: https://github.com/crossforward/rpm-status
Thanks! I’m planning to add a few more features and put it on the Mac App Store. If that doesn’t work out, I’ll share it on github for sure. In the mean time, you can check out this older project that’s open source: https://github.com/crossforward/rpm-status
Christophe Porteneuve
3:49 pm on June 30, 2011
Permalink
Hey Brit,
Excellent stuff. However, IMHE most people using RPM monitor multiple apps indeed, so an app/host selector would be a top requirement (I, for one, monitor 25+ apps across 3 accounts).
Christophe Porteneuve
3:49 pm on June 30, 2011
Permalink
Hey Brit,
Excellent stuff. However, IMHE most people using RPM monitor multiple apps indeed, so an app/host selector would be a top requirement (I, for one, monitor 25+ apps across 3 accounts).
Muay Thai gear for training purposes, you may want to consider MMA
equipment instead. It does not matter if you are studying Krav Maga,
Kick Boxing, Judo, Karate or Jiu Jitsu the world of mixed martial arts
have given us big benefits.muay thai training in thailand
Using foreman you can declare the various processes that are needed to run your application using a Procfile.
Besides the normal processes (rails, workers, etc.) I’ve found great success running log tailing and watchr through Foreman. Here’s what my current Procfile looks like:
If your development pattern is anything like mine, it looks like:
Edit a test, model or controller: check watchr output
Edit a view, js or scss file: refresh the browser
Here’s what that looks like through Foreman:
This works well with watchr and log tailing output, as they rarely overlap. Even if they do, finding the appropriate output is easy with Foreman’s prepending of the process name and timestamp.
Have any other tips/tricks with Foreman? Let me know!
Recently Marco Arment wrote a post and a comment on Twitter about his feelings on the seemingly ubiquitous ratings solicitation in iOS apps:
If I ever added a rating solicitation in Instapaper, it’d be buried in the Settings screen somewhere. But I still probably wouldn’t.less than a minute ago via Twitter for MacMarco Arment marcoarment
Believe me, I understand his reluctancy because I felt it too. If you had asked me two months ago what I thought users think about these, I’d say they thought it was spammy, obtrusive, and just plain shady. But then I found myself implementing it in Key Ring in our latest update. Oh, how my feelings have changed.
The Vocal Minority
I’ve heard that review systems tend to expose the vocal minority, and I believe this. It doesn’t matter if it’s Amazon or iTunes, the vast majority of people who are satisfied, or heaven forbid happy, with a product will never take the time to write a review or leave a rating.
But, there’s a small and extremely motivated group of people that are willing, nay eager, to sling around 1-star reviews at the slightest hint of inconvenience. They have a forum, and by God they will use it. This affects the sleep patterns of app developers in ways I never fathomed until I became one.
The Lazy, Content Majority
The ratings solicitation is the app developers’ last bastion of defense against the Vocal Minority. Key Ring was a meh-inducing 3 stars for the first year of its existence, and our reviews were pretty evenly dispersed between 1 star and 5 stars. After implementing a very unobtrusive, one-time, opt-in ratings solicitation, magically Key Ring is now a sturdy 4.5 stars! The 5 star ratings are through the roof.
Oh how I wish I could attribute the dramatic change to the new UI, speed improvements, or something else I could brag about. Nope, if you look at the written reviews for Key Ring it becomes obvious that the users who are content with the app (who would never give a second thought to rating or reviewing it) are blurting a few words of praise and tapping that last star on the far right just because the app happened to remind them, in a moment of whimsical curiousity and boredom, that iTunes has this rating system and using it might be slightly less boring than whatever they are doing.
The surprising thing is, we have not once had a single complaint or mention about the ratings solicitation. Hell, we get notified by users when we mispell words in our Terms of Service!
The Bottom Line
There are people out there who are completely satisfied with whatever you’re producing, but you’ve probably never heard from them. In an ecosystem that values the coveted “rating” so very dearly, a tactful solicitation for a review is a great way to expose your fans and let potential users of your app know that it is worthwhile. Don’t be afraid to implement one, and don’t think less of apps that do.
The internet is still a very young ecosystem of immature techn… Woah, the BS meter pegged there for a second. Forget the pontification, let me just spit out the two technologies that get me excited about billing myself as a Web Developer.
WebGL
This is a new spec that’s being developed by the Khronos Group (the group behind OpenGL) to expose OpenGL to browsers through the canvas element.
Why is it exciting? Three words: Javascript. Hardware. Acceleration. Damn, I need three more: In. The. Browser.
The concepts should speak for themselves, but if you still need some convincing follow the steps below and be sure to take a look at your CPU usage while running these demos. (Hint: CPU usage will be very low because you’re riding your GPU).
Grab the latest nighly of Chromium here if you are not already running it.
Follow this link to some examples of WebGL in action.
Again, check your CPU usage while running the demos. It should be very low (if you have a GPU) as the demos should be hardware accelerated.
Web Sockets
Comet? Long-Polling? Forever Frame? In two years we will all be sitting around a trash barrel fire laughing about the days of yore when we had to use such hacks.
Why are we huddled around a trash barrel fire? Because it’s post-apocolyptic New Zealand, the last bastion of humanity. Anyways, that’s another “In This Decade” blog post…
[The Web Socket Interface] defines a full-duplex communications channel that operates over a single socket and is exposed via a JavaScript interface in compliant browsers
Here’s the spec in case you want to read it… haha, me neither.
Again, the concept here should speak for itself. Two-way communication with web servers in an easy-to-use interface.
var websock = new WebSocket("ws://www.websocket.org");
websock.onopen = function(evt) {
console.log(evt)
websock.send("Hello Web Socket!");
};
websock.onmessage = function(evt) {
console.log(evt)
};
websock.onclose = function(evt) {
console.log(evt)
};
websock.close();
Fortunately, there are already many projects implementing the web sockets protocol. To name a few,
One thing to note is that the web socket protocol will adhere to the cross-domain security restriction that XHR has. The good news is it will ship with support for server-side origin discretion using the same Origin headers in CORS.
It’s all about the gaming, stupid
I’m going to predict that in 2 years, we will see current A quality games developed in-browser ontop of these two technologies. Don’t get me wrong, AAA quality console and PC titles won’t be disrupted like the music and print industries any time soon. But, the “casual” label on browser games will go away.
This is what has me really excited about being a web developer in this decade. Parity with desktop development is inching closer, and all ontop of open technologies.
peterlubbers
12:44 pm on January 11, 2010
Permalink
Nice post! I'd like to add Kaazing WebSocket Gateway to the list of implementations. It's a production-ready WebSocket Gateway. The best part? It has emulation for all the older browsers (Anything other than Chrome right now), so you can already use WebSocket APIs today.
Nice posting! Games using WebGL and WebSockets is a nice perspective. I made a RTS-game demo using WebGL http://pl4n3.blogspot.com/2010/03/towards-webrt…, I also plan to integrate websockets for the multiplayer-part.
Hey Pl4n3 — awesome that you're working on an RTS in WebGL! Your demo throws an error in Webkit nightlies, though — “Error: 0:3: 'uniform' : cannot initialize this type of qualifier” (with wegGL enabled)
Thanks, also for the RT The RTS currently runs here with Firefox 3.7 (Minefield) nightly. Yesterdey it also worked with Chrome, but today WebGL could not initiate there, strange..
evilhackerdude
8:07 pm on March 16, 2010
Permalink
Hey, I tried the RTS demo with the latest Minefield nightly (3.7pre4 I thin), yet I got exactly same error as Brit.
Hi, i just updated Minfield to the latest version here (3.7a4pre), and the demo runs without problems. Currently cannot reproduce the error :/ Hopefully later, as WebGL gets more stable, this might resolve itself.
Aha great post this, cheers, helps me get started here.
Yes it seems that the WebGL framework hackers, having got things drawing, are rightfully turning their attention now to the backend. I just added Sockets to SceneJS as well: http://scenejs.wikispaces.com/SceneJS.Socket – early days – comments appreciated!
Pl4n3 – whoah – looking forward to seeing how you implement the game logic/protocol etc! PS Your demo works good for me in Chromium build #51510
It's so tough to encounter right information on the blog. I realy loved reading this post. It has strengthen my faith more. You all do such a great job at such Concepts… can't tell you how much I, for one appreciate all you do!
Nice post, however i think you are being very optimistic here. I think this will rather take 4-5 years instead of 2. Especially since your post in now roughly a year old and not much has changed, the next browser generation still hasnt arrived in a stable form.
Furthermore id like to add that most modern games use UDP while WebSockets use TCP/IP which is not well suited for games. You might do some simple games with that, but any real time multiplayer games dont work that well.
Still interesting tech, but it will take some more years to mature.
Curt 7:40 pm on May 2, 2012 Permalink
Hi, nice write up. You have helped me understand the difference between these 2 seemingly similar techs. I think that SPDY should find a new term besides Push as it adds confusion and does not describe the act of sending multiple resources simultaneously.
Bill 9:24 am on May 15, 2012 Permalink
I concur, excellent write-up. Note that you meant “complementary”.