Archive for the ‘Web development’ Category

Hello Bonjour

Hello Bonjour is a new tool for localising website content.

At These Days a lot of the websites we build have a Flash or Flex interface with content fed in via XML documents. That makes it really easy to deploy the same website in multiple languages. You just pass in a localised XML file and voila, you’ve got a localised site.

Until now, we have been translating those XML files manually, but we’ve just made a little tool called Hello Bonjour that makes that process easier. The tool generates an interface with a Rich Text Editor for each node in your XML document. It shows the original copy and the translated copy side-by-side for easy comparison. You can customise it for each project with your client’s official font faces and brand colours to make it easy to generate content that fits the styleguide. There’s also a Find and Replace tool so you can quickly update URLs or other text throughout the entire document.

You can test out a demo here. Sharing is caring, so here’s the source code. Feel free to use it or adapt it to meet your needs.

Hello Bonjour - content translation tool

Red5, will you be my Valentine?

I am somewhat late perhaps, with my profession of love, but last week, we launched a little Valentine’s Day campaign for our clients. We used a great open-source alternative to Flash Media Server (FMS), namely Red5. Red5 has been around for a couple of years now, but this was only the second time I actually got to work with it and I must say that its ease-of-use is simply amazing. Installing the service is pretty straightforward and once you have your server up and running, you can develop applications in a matter of minutes.

Installing the Server

You can download the Red5 server package from Google Code (current version: 0.9.1 final). Once it’s downloaded, just run the executable and it will automatically install the files and then prompt you for an IP address and a port number. When setting up the server for local testing, I just use 127.0.0.1 and 8080.

Once the installation is finished, you might want to run services.msc and check if the Red5 service is installed correctly and is running. With that done, we can open up a browser window and browse to http://127.0.0.1:8080 and you’ll see the Red5 gateway. Here you can test if Red5 is running properly and you can install some of the default applications that come with the Red5 installer. Red5 runs on Java and these applications are great for people like me, who don’t know Java and want to get right down to the good stuff. You can just install one of these basic applications (I recommend installing the oflaDemo app) and once it’s installed, your front-end application can just connect through one of these.

So how about we get to the interesting bit?

Creating your First App

Creating an app in Red5 isn’t too hard if you’re familiar with the NetConnection and NetStream classes, but you can save yourself a lot of trouble if you think your application through.

I created two classes that, in my opinion, came in quite handy:

  1. Red5Connection, the Red5Connection class is a subclass of  Flash’s NetConnection class and is pretty much just a class that handles the events that a NetConnection might throw at you.
  2. AbstractClient, just an extension of a Sprite or DisplayObject. Since we needed three entirely different clients (a broadcaster, a subscriber and a simple chat client, which displayed messages sent by the other clients), it was important to have one base class with the basic logic for all three clients.

Both of these classes are available for download here: Red5 Example files

When working with Red5, you’ll use two things often: a NetStream and a Remote SharedObject. The NetStream is used to broadcast or stream video footage. A SharedObject is used to store or retrieve data remotely. We use this to broadcast data to all connected users (could be used in a simple chat client for instance)

Here’s how you would normally use the NetStream object:

_ns = new NetStream(_nc);

// get the first camera
// set the max bandwidth used by the stream to infinite and the quality to 95 (0-100)
_cam = Camera.getCamera("0");
_cam.setQuality(0, 95);

// get the first microphone and if there is one, make sure there's no loopback nor echos
_mic = Microphone.getMicrophone(0);
if(_mic){
_mic.setUseEchoSuppression(true);
_mic.setLoopBack(false);
}

// attach microphone and camera to the netstream object
_ns.attachCamera(_cam);
_ns.attachAudio(_mic);

// publish the NetStream to Red5.
// the publish method uses two parameters, a name and a type
// the name is used to identify the netstream
// the method determines whether the netstream will be recorded, appended to an existing stream or just streamed live
// possible values are "record", "append" and "live"
_ns.publish("broadcastExample", "live");

If you wanted to stop a broadcast, you could use these bits of code:


_ns.attachCamera(null); // just removes the camera
_ns.attachAudio(null); // just removes the microphone
_ns.play(false); // stops the broadcast but leaves the NetStream intact
_ns.close(); // closes the NetStream altogether

And this is how we use the SharedObject’s send() method to send data to and from all connected clients


if (_so) _so.send("methodName", parameters);

An implementation of this code can be found in the Broadcaster and Subscriber files in the zip file I provided.
To recompile the .fla files on your computer, you will need to download De Monsterdebugger and adjust the class path settings via File > Publish Settings…

Furthermore, you will need to run Red5 locally (or adjust the RED5 constant in AbstractClient.as) and have the oflaDemo app installed in order for the files to work.

Well, that’s it for me, hopefully I was able to persuade at least some people to look into Red5.

Getting the href value with jQuery in IE

I ran into a ‘bug’ in jQuery that only occurs in IE and under specific circumstances. Actually, it’s not really a bug in jQuery… it’s more a funky “feature” of our beloved* browser: Internet Explorer. (* yeah right, muaha :P )

In jQuery you can easily get the href attribute value off of a link by doing $("mylink").attr("href"); but sometimes in IE this isn’t reliable. For example: normally, if the href value is “#myAnchor”, jQuery will return —surprise, surprise— the string: “#myAnchor”. But in some cases with IE, you’ll get the full URL path of the page you’re currently on, with the anchor string value appended to it (for example: “http://labs.thesedays.com/#myAnchor”).

This bug is specifically annoying when you use the href value in a selector in jQuery. For example, you should never do the following, because it may result in unexpected results when browsing in IE: $("a[href='#myAnchor']").click(...);

Workaround? Instead of using the href attribute, you could use the rel or xref attributes of a link, but these attributes aren’t supposed to be used for this purpose and if you disable javascript, the anchors won’t work anymore.

The  jQuery devs should fix this issue ASAP, because I think that issues like these are the main reason why people use javascript libraries like jQuery: they make hacky javascript work!

For me, this proves once again that Internet Explorer totally fails at DOM management.

Two Flash Player Instances Accessing the Same ExternalInterface Method

We’re here again with a quick post from a busy battlefront. We’ve been hard at work on some pretty awesome projects, a few of which we’ll discuss in more detail once they’re finished, but I wanted to take the time to post about a baffling issue we encountered working with Javascript and the ExternalInterface class in AS3.

For an oncoming Nokia campaign we’ve been working on an interactive website that relies heavily on the communication between Javascript and Flash. It involves two instances of Flash Player to interact with the same bit of Javascript. We had our two swf files report to Javascript using a rudimentary event dispatcher (pretty much just a Javascript function called dispatch, that could take one or more params, one of which was the event type)

Now, the issue we encountered was that, sporadically and presenting without any recognizable pattern, the event passed on from the second of the two Player instances, would fail. We just didn’t receive any notice that anything had happened at all.  Even more baffling was that the issue only occurred in Firefox on a Windows PC.

After banging our heads against the walls and endless debugging, we finally discovered that the problem lay in the fact that we were calling the same Javascript method from two separate instances. Once we created a separate function to handle the events for both player instances, the issue resolved itself.

If anyone has any explanation as to why this would happen, we’d love to hear it from you :)
To me, it seems that Firefox is more and more becoming an endless cesspool of eternal doom for Flash developers.

jQuery basics

Yesterday I gave a presentation for students of KDG on the basics of jQuery. I didn’t quite know how technical I could go in my presentation so I kept it very basic. The presentation was —hopefully— a good trigger for them to get started with learning jQuery.

jQuery is an awesome tool in these modern times. Point is, javascript has been around since 1995 and although loads of things have happened… not much has changed since then. Using javascript these days is basically the same as 10 years ago. What has changed is the way we use javascript. It’s because of javascript libraries like prototype js that we can all use javascript to the maximum of its abilities without too much hassle.

I started off my presentation with an explanation of the DOM, since I think this is key to understanding how Javascript works. Javascript works with the DOM, not the HTML. And just like javascript works with the DOM, it works with CSS. By just playing around with the DOM and CSS you can do really cool effects. Some examples where I used jQuery to the max are Nokia Comes With Music Launch and the “flappy thingie” on Sistr.it.

Disclaimer: My presentation is more an introduction to jQuery for the students of KDG. There are loads more things that I could talk about like, for example, chaining. If you want to learn more about jQuery I suggest you check out this nice presentation or buy a good jQuery book ;)