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.
Complementary
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!
Murali Suriar, Brit, Guest and 2 others are discussing. Toggle Comments
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.
Thank you for a very good overview of SPDY, websockets and their relations.
(But you should really change “complimentary” to “complementary” – with compliments Joe
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!
For the past few months I’ve been hacking away at a game partly, well, to make a game, and partly to play with a bunch of web technologies I’ve been interested in. That game is Forgecraft! Check it out and tell me what you think.
What is Forgecraft?
If you’ve played bejeweled you’ve got an idea of the primary game mechanic: move gems into patterns. If you’ve played Minecraft you’ve got an idea of the gameplay twist: patterns. You’re not just matching 3, but moving ores into patterns that resemble weapons and armor. And finally, if you’ve played any loot-based game ever you’ve got an idea of reward structure: loot!
The target player is the casual player, but the game may also appeal to people who just like loot. And the target platforms are any modern web browser. Right now things are working ok in Chrome, Firefox, and Safari. Oh, and the game plays well as an installable web app on the iPad. In fact, all of the design decisions were made with this play mode in mind. Go ahead and save-to-homescreen on your iPad!
Warning: It’s rough around the edges. There’s no tutorial and there may be bugs/features just flat out missing. I consider the game’s state as ‘playable demo.’ But do let me know if you run into any bugs.
The Tech
Over the next few months I hope to put together a series of posts on the different technologies I played with while making Forgecraft. You know, go in-depth on each of them and all that jazz. For now, here’s just a run-down of the fun stuff I used:
My plan is to continue developing Forgecraft, expanding the items, mines, etc. and digging further into these emergent HTML5 technologies. I also would like to continue improving performance across the board. Things can get pretty choppy on the iPad when there are a lot of animations running at the same time.
Please, give Forgecraft a try and let me know how you like it and if you have any suggestions. The number one question I have is, is it fun? A game should be fun, I’m told.
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.
Speaking of coordinate systems, the following 3 part primer from the Wolfire Dev Team is an excellent refresher on linear algebra. I studied this in school, but having been out of the engineering game for a while I find my “bow staff skillz” are shamefully rusty.
Lately, I find myself spending more of my free time working on a little game using Cocos2d. One of the things I was surprised to discover is that there’s not just a single coordinate system — there are four (that I know of — please correct me if I’m wrong).
The four I know of:
Core Graphics space
Open GL space
Node space
Node space relative to anchor point
I wasn’t totally clear on these going in, and I regret it. I ended up writing a lot of code that mish-mashed these and had to refactor it all.
Most likely you’re going to be processing a touch input on an iOS device and translating that to a coordinate system inside of a Cocos2d node (most likely a CCLayer) as such:
Core Graphics (point) — This is what you normally work in if you’re accustomed to iOS development with UIKit and similar APIs. CGPoint(0, 0) is in the top left corner of the screen and ascending Y values go down towards the bottom of the screen.
Open GL (glPoint) — This coordinate system has (0, 0) in the bottom left of the screen, and ascending Y values go up towards the top of the screen.
Node space (nodePoint) — This space is relative to the origin of the reference node with which you are observing the point, and follows the same rules as the Open GL system. If your node is full-screen and unshifted (e.g. a default CCLayer), this coordinate system is exactly the same as the Open GL system.
Node space relative to anchor point (nodePointAR) — In Cocos2d, the default anchor point of any node is its center point. This coordinate system is relative to that center point. If you redefine the anchor point, this system is adjusted.
The big gotcha that I ran into was that I was doing something frowned upon in Cocos2d: shifting Layers! I was then using the Node space throughout my code but expecting points to resolve to Open GL space. So, word to wise: understand these coordinate systems going in, and don’t shift the position of your Layers!
Cocos2D has an inverted coordinate system to what you’re used to – the … Now before we can see this running, we first need to tell Cocos2D to run our …..the entire game scene, more information is available in this thread.
Here are a few key points about the score and the recognizer in general:
Calling detectGlyph will always return a match!
The match returned has a score (float) associated with it
The higher the score, the better
There’s no absolute scale as to what determines a “good” match
As an example, if your only template glyph is a circle and the user draws a squiggly line, a call to detectGlyph will still return a match to your only template. The score will most likely be very low since the two gestures don’t resemble each other. Ultimately, it’s up to you and your application to determine what is a good match and whether to accept the input as a match or not.
Behind the scenes, the N Dollar Recognizer is assigning a score to every template, but only the highest score is returned to the delegate.
So, there’s no magical threshold in the score that determines a match. You’ll have to seed your templates and then do some testing, and your particular implementation will have to define score thresholds.
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!
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”.
Brit 8:21 am on June 29, 2012 Permalink
Ah, thanks for the correction, Bill
Brit 8:22 am on June 29, 2012 Permalink
Thanks, I’m glad it clarified the distinction between the two for others as well.
Guest 2:46 am on July 11, 2012 Permalink
Thank you for a very good overview of SPDY, websockets and their relations.
(But you should really change “complimentary” to “complementary” – with compliments Joe
britg 8:20 am on July 11, 2012 Permalink
Hah, I forgot when Bill had mentioned it — ok, I promise it’s changed now.
Murali Suriar 7:42 am on August 2, 2012 Permalink
Great article; very helpful.
Alas: you changed the section title, but not in text:”No, they are complimentary and will coexist.”"The two protocols are actually complimentary.”:)