Friday, May 18, 2012

DartJSONP for simple JSONP callbacks

At last the London Dart Hackathon last week, one on the questions that I was asked the most was how to access JSON data using XMLHttpRequest that is served up from a different server to the one serving the Dart app.  XMLHttpRequest is part of the magic behind "AJAX", and allows a browser to request data from a server, but accessing data from a different server presents some challenges.

XMLHttpRequests blocked

The issue is this: In the web browser, you use XMLHttpRequest to access data from a different server, in order to create some kind of "mashup" application.  Browser security restricts this unless the remote server returns special headers indicating that this is allowed.  This is shown in Figure 1, below, with a typical JSON request, such as when trying to access JSON data from Twitter: http://search.twitter.com/search.json?q=dartlang



Figure 1. XMLHttpRequest response data is blocked by browser security

JSONP workaround

This has already been solved in JavaScript by using a callback.  The data returned is wrapped in a JavaScript function call (known as a callback), so that when the data is received, the browser calls the callback function, passing in the returned data.   This is known as JSONP, with the url now having a new callback parameter (specifying the callback function name), for example: http://search.twitter.com/search.json?q=dartlang&callback=onDataReceived

Using JSONP with Dart

Unfortunately, this is trying to call a JavaScript function, and not a Dart function.  At present, the only way to communicate between JavaScript and Dart in the same application is by using window.postMessage in JavaScript and adding a handler with window.on.message.add( ... ) in Dart.  Seth Ladd discusses how to achieve this for JSONP callbacks in his blog post, and the flow is much like that shown in Figure 2.


Figure 2 - Using JSONP with a JavaScript callback and window.postmessage

Introducing the DartJSONP library

This all works fine, but you need to write some boilerplate code, such as manually adding a JavaScript callback function to your html, and writing a postMessage handler to receive the data.  If you want to receive data from multiple, separate sites, you also need to design a way to tell the difference between the different callbacks sent by postMessage.

In order to make this simpler, I have created the DartJSONP library, which automates the task that Seth describes in Option 2 on his blog post.  This allows you to call out to multiple servers and get JSONP data back with minimal code.

The following example shows how you can access some Twitter data using DartJSONP which defines the class JsonpCallback.  You create an instance of the object, passing in the name of the callback (which can be any valid function name).  Then when you call the doCallback(url) function, the DartJSONP library takes care of all the JavaScript and postMessage handling, simply returning the data in the onDataReceived handler.


var callbackFuncName = "twitterFunction";
JsonpCallback twitterCallback = new JsonpCallback(callbackFuncName);
twitterCallback.onDataReceived = (Map data) {
  // do something with the returned data
};

var twitterUrl = "http://search.twitter.com/search.json?q=dartlang&callback=$callbackFuncName";
twitterCallback.doCallback(twitterUrl);


Internally, JSONP will dynamically add the JavaScript callback function, invoke the call to the remote server, post the response back to JavaScript, which posts the data back to Dart.

You can see this running by navigating to this link: http://example.dartwatch.com/jsonp/DartJsonPtest.html which loads data from Google Plus and Twitter (it's the example that is used in the test currently on github).

JSONP is available on Github.

References:
Seth's blog post where the JSONP and postMessage handling is described
MDN documentation on CORS - which is how web servers can avoid the requirement for clients to use JSONP



Wednesday, May 16, 2012

Dart in Action is Manning's deal of the day


Dart in Action is Manning's "Deal of the Day" 


Deal of the Day for May 16th, 2012
Get half off Dart in Action. Enter dotd0516 in the Promotional Code box when you check out at manning.com.

Valid from Midnight Eastern Time, May 16th for 24 hours only.




Dart in Action is currently available as an "early access" publication (MEAP), which gives you the opportunity to provide feedback and influence the book.  4 chapters are currently available, two more are currently with the publishers, and I should be working on chapter 7 (instead of typing this!)

Tuesday, May 15, 2012

Weekly Dart community update – week ending 15 May

Dart weekly news update.  Extended this week, due to the Dartwatch server outage.  News from Dart Hackathons, Libraries and Blogs.

Hackathon News:


Jai Luthra shared his Rock Paper Scissors Game: (G+)
London Dart Hackathon:
Ukraine Hackathon:

Events:

Blog Posts


Adam found (G+)this dart podcast from the Silicon Valley hackathon:

Dart Libraries and Examples


Adam tidied his (G+) websocket chat example:
Justin Fagnani created a static file server (G+)



Dartwatch up and running again, and apologies for the RSS spam

Dartwatch up and running again, and apologies for the RSS spam


Following the unfortunate incident with my ISP's RAID array, I've now pretty much transferred the site over to blogger.  I've still got to move the images over, and changing the RSS feed on feedburner has triggered all the posts to be re-issued - sorry for that!

In terms of timing, it couldn't have been worse, having been the weekend of the London Dart Hackathon, so apologies to all those who who tried to access the community packages list.

I'm catching up with everything now - normal service is being resumed.


Storm Internet: Status Update - 12/05/2012 15:14:13
WinSrv104 hosted at IP address 94.76.230.101 has suffered a catastrophic disk failure. This has been caused by a failed RAID array which has corrupted all three hard disks on the server. We are now in the process of setting up a new temporary server in order to get sites and hosted services back online ASAP. Once the server is setup we will begin restoring web sites from backup copies we hold. We estimate this process to complete within the next 18 hours. The reason for the lengthy delay is due to the amount of data that needs to be transferred. 

 (It took them the whole weekend to get back up and running again).

Monday, May 14, 2012

Dartwatch.com hosting problem

Hello everyone, This weekend, my ISP suffered what they're calling a catastrophic failure on a RAID array.  You can read the details here.

In the meantime, I'm hosting some of the more popular pages from the Dartwatch site on this blogger page - I'll probably migrate the whole site here (in a classic case of shutting the door after the horse has bolted).

Thanks for your patience.   In the meantime, you can still grab code samples from Github: https://github.com/chrisbu/

Update:  I'm going to permanently move everything to this new url: http://blog.dartwatch.com - it might take me a few days to do it, though.

Monday, May 7, 2012

Sign up for the London Dart Hackathon, next Friday 11th and Saturday 12th May.


Sign up for the London Dart Hackathon, next Friday 11th and Saturday 12th May.

Next Friday (11th May), from 7pm, London Google Technology User Groupand London Dart Community are hosting, with support from Google, thefree London Dart Hackathon, part of the Global Dart Hackathon event.
It is a great opportunity to spend some dedicated time hacking Dart, in the company of some Dart Engineers from Google, who will be on hand to help and answer questions, as is +Seth Ladd, Google’s developer advocate who is coming over from Mountain View for the event.
The event is ideal for both individuals or teams – there will be a chance to join a team if you are coming on your own, but please register all attendees at eventbrite if you are coming – it helps with planning.  Dart is still a tech preview, so this is a great chance provide valuable feedback on the development of the language.
It is taking place at CampusLondon, round the corner from Old Street station (2 tube stops from St Pancras) and the schedule is as follows (from the London GTUG site): 
FRI 19:00 – 20:00 – Orientation and Kickoff
  • Welcome video
  • General guidelines for the event: schedule, wifi password, GitHub repo etc’.
  • Intro to Dart by Dart engineer(s)
FRI 20:00 – 20:30 – Team pitches
  • Each team will quickly present their demo idea and solicit new team members.
  • The hackathon will be team based, but since not everyone will come with a team, this portion of the event allows each team to take 60 seconds to pitch their idea and solicit team members.
FRI 20:30 - Hacking 
  • It will be an ‘open bar’ style so people can come and go as they wish during the evening.
  • Light snacks and drinks will be available.
SAT 8.00 - 17:00 – Hacking 
  • Continued ‘open bar’ style hacking all day until 17:00.
  • Light breakfast from 8.00-9.30.
  • Lunch at 12:00.
SAT 17:00 – 18:00 – Demos 
  • Each team will demo their hacks for the group and judges.
  • Each team has 5 minutes to show off what they built.
  • The judges may take another 1-3 minutes to ask questions.
SAT 18:00 – Prizes 
  • The event coordinators and judges will choose the winning team based on the best technical achievement.
Links:
Global Hackathon Site (FAQ, Learning Resources etc…),

Saturday, May 5, 2012

Weekly Dart community update – week ending 05 May 2012


The first in a (hopefully weekly) roundup of community Dart activity on G+ (and elsewhere if people let me know)
Dart Flash library gets an update (G+):
Here is the weekly update for the #dartflash library.
* Added Shape and Graphics class. (Demo http://goo.gl/okoSH).
* Bugfixes
Use the #dartlang programming language and your Flash skills to build new games or port your exiting games to open web standards.
SQLJockey gets to a reasonable state (G+)
John Evans explains how to contribute to Dart (G+)
Ido Green created a dart crawler  (G+) example
Also on Ido Green‘s blog, an Instagram example (G+) was created from a hackathon:
Dart Snake app was created as an example from a hackathon:
Dart Music app was created as an example (G+) from a hackathon
Darthack Prague publishes it’s repos on github:
Matthew Buttler publishes his updates to a Dart MUD:
Apologies if I’ve missed anything.  Let me know, and I’ll include it in next weeks roundup