WebSocket Charting Demo with HTML5 and JavaScript

One of the PHP demos that goes over pretty well is my Flex socket demo. It used to be that Flex/Flash was really the only way to take advantage of WebSockets but with browser vendors implementing bleeding edge support for HTML5, developers can now use the WebSocket API in JavaScript. I wanted to combine that with the Canvas API and try to recreate a basic example of my Flex socket demo in HTML5/JavaScript. It mostly works (demo embedded at the bottom).

Code

  • socket.html – HTML/JS file for connecting to the socket server
  • socket.php – PHP file that creates the socket server

Who Can Run This

It’s been kind of cool to see the browser vendors really move forward to implement support for various bits of HTML5 and CSS3 without a defined spec. That can be problematic because the spec is still changing, causing code to break in new versions of browsers, but in general it means that web developers can live on the cutting edge for most browsers. Currently, the WebSocket API (and this example) should work in the latest version of Chrome and the Firefox 4 beta.

Differences in WebSockets with Flash and HTML5

Overall the WebSocket API is pretty easy to use. I took my existing PHP socket server code that works in Flash and tried to use it for the HTML5 version but all I was getting was the “close” event. The problem was that I wasn’t thinking about the handshake. Flash uses a policy file to determine whether or not it can connect, but the WebSocket API uses a handshake. I was able to grab some example code, change my PHP server to add the handshake then everything basically worked. The only other small change I had to make was to add a character (chr 0 and chr 255) to either end of the message I was sending through the socket_write method. I’m still not entirely sure why this is, but my onmessage event wouldn’t fire until I added those.

Charting with Canvas

I’m very excited about the Canvas API in HTML5. If you’re a Flash developer who has been working with the Flash drawing API, you’re going to be able to do some very cool stuff. The APIs are fairly similar with some syntax changes that you have to be aware of. The biggest pain I found was that once something is on the canvas, it’s on the canvas. You can’t reference specific drawn elements like you can in ActionScript. That makes doing things like the hover effect on specific charting points basically impossible.

Partly because of that, and because I’m still getting used to JavaScript I went with a very scaled down version of the chart. All this chart does is draw some grid lines and then plot the points as it gets them from the socket server. When you get to the end, the points are drawn off the page. I also couldn’t really figure out how to add to a path in a function call so I just went with the points instead of making it an actual line graph. I’m fairly sure that most of these are due to my JavaScript incompetence and not a limitation of Canvas.

Conclusion

It was both a lot of fun and very painful to go back to JavaScript. It’s an incredibly powerful language but if you come from Flash, you’ll find yourself banging your head against the wall because of subtle differences. The one thing that keeps tripping me up is figuring out how the DOM works compared to Flash. Another sticking point is the tooling. We have very good tooling on the Flash side compared to JavaScript. Chrome’s developer tools and Firebug both help a lot, but there’s no tool out there that provides code completion for the Canvas drawing API. When you don’t know the API that well, it means a lot of Googling, and a lot of the examples are pretty basic. I think a tool with basic code completion for Canvas would make it a lot easier to start creating complex content in.

It’s definitely rough to go from Flash to JavaScript but hopefully I’ll be playing around with the new stuff in HTML5 more, especially as Dreamweaver gets more support baked in.

Related posts:

  1. “We’re Going to Try To Make the Best Tools in the World for HTML5″
  2. HTML5 Versus Flash Versions
  3. On Google, YouTube, HTML5, Adobe, and Flash
  4. AIR for JavaScript Developers is now Available for Download
  5. Adobe Tools and HTML5/CSS3
  • zcorpan

    “The only other small change I had to make was to add a character (chr 0 and chr 255) to either end of the message I was sending through the socket_write method. I’m still not entirely sure why this is, but my onmessage event wouldn’t fire until I added those.”

    This is because that’s how the websocket protocol is defined. Messages are framed and start with a 0×00 byte and end with a 0xFF byte, and between is utf-8 text.

  • ryanstewart

    Thanks @zcorpan. Any reason why they defined it that way? I’ll have to go dig and see if I can find out.

    =Ryan
    ryan@adobe.com

  • http://www.reigndesign.com Todd Cullen

    Just wanted to point out that a single canvas element is more akin to a Flash Sprite than a whole Flash Document. If you want hover states etc. you’ll need multiple canvas elements on the page to provide the Flash-like interaction you’re looking for.

    But you can port some pretty sweet Flash animation straight over >> http://www.reigndesign.com/blog/converting-flash-animations-to-canvas/

  • ryanstewart

    Thanks Todd, that’s the impression I got. I figured that multiple canvas tags would slow things down and be hard to manage, but maybe that’s not the case. And yeah, I think there is a lot of potential to port some of the Flash goodness over to canvas pretty easily.

    =Ryan

  • JulesLt

    Your other alternative would be creating / modifying SVG on the client side – that actually creates a DOM interogatable & modifiable object (so you can add event handlers to particular points, modify elements).

    http://www.ibm.com/developerworks/library/x-svgclientside/

    I know you’re just using a chart as a proof of concept for linking Canvas and WebSockets, but for charting this is probably the way to go.

  • ryanstewart

    I had thought about that after I was 3/4ths of the way finished getting the canvas working. At the time I wasn’t sure how SVG fit into the page (or that it used the object tag) but I think you’re right, ultimately, for charting, SVG seems like the way to go. And there’s more of an Adobe tool story there as well :)

    =Ryan

  • JulesLt

    Yes – people forget that one of the most significant parts of ‘HTML 5′ is an Adobe invention – and of course Flash can be used to enable support on older versions of IE, although presumably without it being visible as a DOM object.

    A good starting point :

    http://srufaculty.sru.edu/david.dailey/svg/SVGAnimations.htm#JSAnimarative.html

  • Tyler Jones

    Ryan, will you please explain why you’re now promoting JavaScript over flex/flash? There are going to be plenty of Javascript how-to’s and blogs coming up, why take one of the official voices/evangelists of the flash community (you) away to promote JavaScript? There’s so many great uses of flash that we have yet to be shown, why not continue to show us new flash stuff that we’ve never seen? If the official promoters of flash from adobe are taken away, then who will continue to stand for flash? There are going to be PLENTY of companies promoting JavaScript, why does adobe need to start promoting it as well? Those other companies are not going to start promoting flash the way adobe is trying to promote the spread of JavaScript RIA’s.

  • ryanstewart

    Tyler, I wouldn’t read too much into the post. I think the Flash way of doing it is much, much easier. I also like to spend time learning new web technologies and HTML5 has a lot of momentum.

    What it ultimately comes down to is that I feel like I can be a better evangelist for Flash if I know how the technologies around it work. So this is just a sandbox for me to mess around in.

    =Ryan
    ryan@adobe.com