Keep track of all the latest news and events on developer tools and technologies you care about
In the classic 1939 movie The Wizard of Oz, Dorothy has just arrived and is looking around awed at the splendour. She says to her dog “Toto, I’ve a feeling we’re not in Kansas any more”. Well some of you seasoned Microsoft developers are going to feel exactly the same way when you get to the gates of developing your first Windows Store app.
Obviously, it’s different from what you know and love as .NET developers and Windows Store apps are different to those many programs that are now rather cruelly described as “legacy apps”. It’s different in some fairly obvious ways but there’s also some other perhaps more subtle “gotchas”. I’ll try and cover all these here.
Gotcha 1: You can’t just assign any old string to innerHTML.
You have to turn it into a special sort of trusted string by passing it through the toStaticHTML method. And if the string contains something like <button onclick=’handleClick()’>Press me</button> then the onclick handler will get stripped out.
There are various other ways round this restriction which is intended to stop script injection attacks.
Thus some (imaginary) code to open a file might go openFile(“name”).then(readFile,handleError).done(doSomething,handleError) There are two useful calls that can act on a promise, then and done – more details here. They take success and fail functions and it’s very important that you always provide them both, as you find yourself in
Gotcha 2: When your program isn’t in the foreground it isn’t running.
So timeouts are very likely to happen. The user requests some data that causes a network transaction, and then slides to read their mail. They find lots of interesting things there and spend maybe half an hour or so, and then return to your app. You won’t have run for 30 minutes and your TCP/IP connection may well have timed out.
There are ways to have your app run in the background, but not more than every 15 minutes. In the interest of battery life and minimising network traffic, in general when the user isn’t looking at your app then it ain’t running.
How many times have you seen a little spinning ball or a message “Waiting for server” or a terse message “Can’t use this app as not online”? I don’t like these and I’m sure other users are equally unimpressed.
Gotcha 3: You have to work well offline
Windows 8 actually provides you with no excuse not to offer a decent offline experience – that’s the whole point of the promise mechanism. You set up a transaction of some sort and then allow the user to carry on working, interacting with your app. Of course this may cause another transaction to be queued, so you will need to keep track of the replies when they come back, but you’ll have a happy user.
Consider my favourite scenario. A user is sitting on the Tube, using your app to read messages or tasks and then making suitable replies. If they are between stations then there’s no signal, and they certainly don’t want to be confronted with a “Please wait” message once they have finished their first task. Instead they want to carry on through the list. As the train comes into a station the WiFi connection is made, the data waiting to be transmitted is sent and any new messages are uploaded. All in the background, automatically.
You may well have used a quick alert(val) to find out what some value is taking – well surprise!
Gotcha 4: You can’t use alert()
If you are still with me you aren’t going to believe the next bit, but trust me we have tested this extensively. If you are writing for a mobile device such as a Microsoft Surface then you can’t trust that network stalwart, TCP/IP, to deliver your data reliably.
Gotcha 5: You can’t trust TCP/IP
Hush while I hear that intake of breath. The man’s insane of course TCP/IP works. Well it does, very well, between two IP addresses. But devices that you can move around between WiFi hotspots (and cellular networks when that feature is provided) get a different IP address every time.
Consider sending an email. You press send inside your favourite WiFi enabled coffee shop, shut your Surface and head for the door. The mail program has probably had time to hand the data over to Windows 8 which will be using TCP/IP to send your message to your mail transfer agent. But the WiFi connection is busy and there’s lots of retries needed – normally you would only see this as a slow transfer. But you are already heading down the street and the IP address allocated to you by the coffee shop WiFi hub isn’t going to work until you go back there. In the meantime you head into work and get a new IP address. The TCP/IP connection between the mail program and the coffee shop will eventually time out, and the data remaining in the TCP/IP pipe will be lost.
Now in fact your mail program uses another protocol called SMTP on top of TCP/IP (mainly because it used to work over other protocols that weren’t reliable) and so that mail will remain unsent in your outbox and will be resent when you open your mail program again. But naked TCP/IP connections aren’t so forgiving, so we come to
Gotcha 6: You can’t trust Ajax
Say you set up a call to send data via Ajax to a server. The HTTP message is sent and you wait for the reply. Given the scenario outlined above you may well get a timeout error – possibly much later depending on various factors. Now what do you do? Perhaps the data was delivered to your web service and you lost the reply, but on the other hand it’s just as likely that the data didn’t get there – or more accurately all of the data didn’t get there.
So what do you do? Some badly written apps simply put up a message saying “Timeout error” and leave the poor sucker user to work out what happened. Of course you could automatically send the message again, and if you knew that there wouldn’t be any trouble by doing so then that’s normally a good idea. So if the web service included a transaction id, or was otherwise idempotent then this would be fine. But many web services calls will do something twice if you call them twice, and hence this leads to trouble when resending Ajax data from mobile devices. And of course if you don’t resend the message then you will risk losing transactions held in TCP/IP streams to old IP addresses that are slowly timing out.
The web service has a similar problem. It has data it was trying to send as the result of an Ajax call but the TCP/IP channel times out. There’s nothing much it can do with the data other than throw it away and hope that it can be recreated again. But that data might be important - the receipt for an online transaction that has taken place, for example.
The problem is even trickier in the other direction, such as when a backend server wants to send data to a mobile via TCP/IP. Let’s say you have a multi-player game which is sharing co-ordinates of players over TCP/IP. You send some data which is held in buffers in the server’s OS trying to be sent to an IP address in a coffee shop somewhere. It’s never going to be delivered and the socket will eventually time out. Your backend system has to handle this situation gracefully and it’s tricky. It’s pointless attempting to re-open the communication channel as there’s no-one at home at the last IP address. It must wait for the app to notice and re-open the channel, possibly sending back a note about how far it had got in the data transfer allowing the server to re-send the data. But this in turn means the server has to keep all the data it has sent so that it can re-send it, and there’s no obvious way that such saved data can be deleted, so you end up re-creating something like SMTP.
But even now you haven’t solved the problem, because your app might be in the background, and as explained above, not running. Or even more likely, the device has turned off its radio to save energy or has completely gone to sleep. Say it’s a chess game and you want to send the move from an opponent. Even without the problem of TCP/IP not working when changing IP addresses you can’t send data to a program that isn’t running.
Gotcha 7: You can’t send data to a device
The solution is for you to arrange to send a push notification to the device via Windows Notification Service, which also requires for you to register the app and extract a special URL which you have to send back to your backend system, which then calls WNS. If you want to use Windows Phone 8 beware that there is another, different push notification system called Microsoft Push Notification Service that needs different values. And of course if you want to make a cross-platform app there’s three other different push mechanisms.
These push mechanisms are not guaranteed to be delivered but are the approved way of alerting the user that they need to run your app.
You will have seen by now that the environment for Windows Store apps is really very different to what you may have been used to. There are several challenges.
· Saving the battery and hence coping with not running when in the background
· Saving network charges by not polling unnecessarily
· Coping with different IP addresses, and hence unreliable TCP/IP
· Managing sending data to mobile devices that may be asleep
For more details on these issues and ways to provide reliable mobile messaging please visit www.fiveapp.com/windows8.
Dr Tim King is a veteran computer scientist: he gained his degree at Cambridge in 1976 and later in 1979 for his PhD he wrote a relational database when this was a new concept. In 1984 he got the chance to write the disk operating system for the Commodore Amiga and can hence claim that more than two million people have used his software. He went on to found Perihelion Software, working with early UK entrepreneurs at Inmos (remember the Transputer?). He then founded ISP UK Online and since selling that in 1996 he has been advising people on whether to invest in various high-tech ventures.
More recently he has spent his time experimenting with mobile phone technologies and is the founder and CTO of 5app, a company that provides a way for developers to create mobile apps that communicate reliably. Here he and his team have spent many happy hours driving phones around the South Wales valleys where he lives, discovering what really happens to your data as the connectivity comes and goes.
He can be contacted via his personal website at www.tim-king.com or the 5app website www.fiveapp.com, via Twitter as @DrTimKing and linkedIn via uk.linkedin.com/in/drtimothyking