Azure Functions for Mobile Apps | The Xamarin Show

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
>> Welcome back everyone to The Xamarin Show. I'm your host, James Montemagno. And today, I'm excited for returning guest and brand new Cloud Developer Advocate, Laurent. How's it going? >> I'm very fine. Thank you. >> How was the trip over back in beautiful Seattle? >> No problem. I was upgraded actually to business class which never happens. >> After flying hundreds of times, I took my first business class flight. >> It's a whole different experience. >> It's something that I did once and will probably never happen again. >> Exactly. >> But it was one of those rare instances. That's awesome. It's good to have you back. >> Thanks. >> The last time you came on, we're talking about MVVM Lights. Some beautiful MVVM goodness. Now, we want to talk about something a little bit different. I'm excited because we've talked a little bit about functions, Azure functions, surrealist compute. But you're like, "James, I did this awesome app. I'm really getting excited about it." So, we are going to be talking functions, but maybe, tell everyone a little bit about yourself because they don't know who this man is. >> Sure. Yes. So, my name is Laurent Bugnion. I was an MVP for many, many years. I was a Xamarin MVP as well, regional director. And in August 2017, I joined Microsoft and they made me an offer I couldn't refuse, literally. And I'm a Cloud Developer Advocate. So, this is a team who is going out and evangelizing Azure, but we don't like to use evangelizing anymore. So, we are advocating Azure, I guess. And so, it's pretty much like being an MVP, except now, I'm paid for it. So, it's a good change and I'm really happy and we have an awesome team. >> That's awesome. You're great fit because not only have you been awesome, and now in the Microsoft community for so long, and I followed you a long time for MVVM Light when I was building Windows Phone apps back in the day and still love it to this day. So, it's good to have you on the team over here. >> Cool. Thanks. >> So, what exactly are we going to be talking about? What do you want to show off with Azure Functions? >> So, what I would like to show is how you can use Azure Functions to build a very, very simple API that you can use for Xamarin applications. And of course, you can use Azure functions for any scene, right? It doesn't have to be a Xamarin application, but what I like in Azure Functions is that they are very easy to deploy, they're easy to build and you can even test them. The whole Shebang in Visual Studio, so that's great. And since they are so easy to deploy, they are very easy to access with an HTTP client from Xamarin application. And so, they are very well suited for mobile applications. The communication is based on text, so you can send some JSON over. You just serialize, de-serialize. And the cool thing with functions is that you can build them directly in the Azure portal. If you want, you can build them with Visual Studio for Mac. If you want, here, I'm going to show Visual Studio for Windows because it's also what I prefer, but the tooling is extensive and you can really choose any tool you prefer. >> So maybe before we get into it, maybe you can describe what exactly is an Azure Function. A lot of people are like, server lists and like micro tiny services and all this. So, what to you is an Azure Function? And why would I want to use it? >> It's a great question and, really, the moniker serverless, it's like marketing, right? I used to joke about that and say the big secret in the industry is that there is actually a server. >> There has to be. >> There is a server. Absolutely. The only cool thing about serverless stuff is that, you don't need to worry about the server yourself. So, you're going to deploy that, you're just going to know which region it works in, because you have to kind of select that from the start, like West Europe or Northern US or whatever. However, after that, your functions are going to be small units of computations. Well, typically, it a class with one method, which is public, which is a run method. And then after that, if you want multiple functions to communicate together, you can do that. There is a whole infrastructure going on, but the thing is that you think of the function as a small unit of computations. So in terms of architecting your services, it's actually a good practice because you don't build a huge web service, web API application with a lot of dependencies, a lot of class libraries and everything, you do that as small as possible. Now obviously, if you want to add class libraries, you can. If you want to use NuGet, you can. If you want to use.NET core, you can. Those functions can run on Linux as well. So, you can choose actually if you want a.NET server, or a Linux server. But again, it's really more about, "Hey, let's forget a little bit about the infrastructure and let's really write some code, deploy that as fast as possible, and then just communicate over REST, typically over HTTP." >> So, I'm used to essentially creating in like an asp.net core website or web API. I've got to scaffold my entire RESTful API. >> Correct. >> And then, access my database. I have to write those queries into the database, then I have to deploy that up into Azure on a server that is essentially running. It's scaled, right? >> Sure. >> Ups and down, but it's on 24/7. That's what I'm used to. So then, what's the difference between what I just described, maybe what a lot of developers are used to, and the flow of getting a function up and running? >> What I will show in the first demo is that you can actually publish a function directly from the portal. So basically, you're going to just create your function application, then you write the code directly into web browser and then it's pretty much already deployed, so you don't even need to publish it, right? >> It's there. >> It's already there. As soon as you save, it's done. The other flow which is a little bit more production-ready would be to write in Visual Studio. So, you create the function application in Visual Studio which actually under the cover is an app service, so it's a little bit like a web service. But again, I think the main difference is that you don't have to worry about the infrastructure, the Azure Function is going to do that for you. So you're going to create your function, you're going to write it, you can unit test, you can debug, you deploy to Azure, and then it's running. Now, the function is not going to run 24/7, so it's really probably the main difference. And in terms of core symptom of financing, it's really good because you're going to be built only when the function is called. >> Got it. So with my normal server, I'm paying for it 31 days out of the month, 30 days a month, 24/7 but I don't know when someone is going to call it. So, if I call this function, is it on? Is it not on? Do I get charged per call to it? How does it work? >> So, there are two ways. You can actually select that when you create the function or you can configure it. One way is called a consumption way, which means that the function is literally going to be built only when it runs. And so, that's going to be a very small amount of money every time that somebody calls it, and so that's a great way to get started. Because in the beginning, let's say you're a startup, you don't know how many users you're going to have. Maybe sometimes, you won't have any users. And so during that time, you are not going to be built. Only the advantage of that is that there is a small cold start. So basically, the function is sleeping at that moment. If nobody has called it for a while, then the first call is going to be a little bit slower, and that's this whole thing. Now, you can switch also to what they call, an app service plan, which means basically you can say, "Hey, I already have a website so I already have an app service plan configured where I'm going to be build so much per month." And then, you can add your function application to that, and this is actually making it a little bit easier to budget how much you going to pay. So usually, what I tell people is if you're a start up, start with the consumption plan. And when you know, when you see that, "Oh my god, I have more users coming," then I want to know exactly how much I'm going to pay every month, then you switch to the app service plan and that's better. >> I like that. I think the first time I ever wrote an Azure Function, it runs so fast. It's hundredths of a millisecond, right? >> Yeah. >> I think, I was watching Scott Guthrie. He did a cost analysis on stage. He's like I just called this and it hit like Cosmos DB Database, processed a bunch of stuff, inserted something in the blob. So he's like, "I paid .000000, one-tenth of a penny." I guess something like that, which is crazy. >> Yeah, that's crazy. >> That's great for a startup because you don't have to worry about server. >> Exactly. >> You don't need to worry about this cost and all that stuff. >> Yeah, and it's going to scale. You can deploy multiple regions so you have all the power of the Cloud. >> Let's see it. What do you got for us today? >> Absolutely. So here, I have a really, really super simple function and I think it's more really for demos than anything else, but the process is that I went ahead. So I did create a resource, I selected an Azure function and it puts me into the web browser here with some code already, and this code is written. And so basically, the process that you get, you get a request. And so, this is the HTTP request message. This is where you get all the information about the color, things that can be interesting like, what browser are they using, if they are using a browser, these kind of things. And then after that, you can get to a number of operands. So in that case, I have two operands, num1, num2. And the way that you configure that, if you go to your function and go into integrate here, you can actually select the root that you're going to use here. So maybe one thing we need to specify when we talk about function, is that you have different triggers that can run the function. So here, I'm using an HTTP trigger and we see that here in this portal. An HTTP trigger means that the function is going to be executed every time there is an HTTP request and those HTTP requests can be a "Get," it can be a "Pause," it can be a "Delete." So basically, you have the whole range of REST APIs. Now of course, you do have also triggers that you can use and one very useful trigger is, for example, "Timer" trigger, where you're going to say, "My function is going to run once every hour, or once every day, or do things, it can maybe store some values." I'll show a demo in a moment. So here, I have an HTTP trigger and I have the root here. And as you see, it's a little bit small. But if I tried to zoom in, let's see here. I have the root so we have /add which is the name of my API. And then I have num1, and then I have these placeholder which is going to be replaced by the color, and so, this will be basically where the first operand is. And then, you have /num2 and then, the second parent. And here, I say, I will call this function only with Get, okay? >> Okay, I got it. So, you can then specify like, hey, all these different functions. I have one for get or put. And you can do either. >> Exactly. >> Now, you said it's a trigger. I mean, a timer seems cool because I can clean up a database. >> Timer is good. >> Can I do other things? >> Yes. So, you can do, for input, a "Blob" trigger. So, "Blob" trigger is, in my opinion, very useful if, let's say, you build a website showing pictures, for example. Every time you upload a picture, a Blob is basically just a file, right? If you want, it's just a binary of some kind and so, when you upload it, the Blob triggers, then there is a rule that you specify. You say, "Okay, now, I want to watch this Blob container." Every time something is uploaded, the function is going to run and that can be a good way, for example, to extract some geobased information from the picture, or extract the xfig tag, these kind of things. You have also things like WebHooks. So basically, you can Hook deep to different websites. There is one which can be very useful. It's a GitHub WebHook, so every time you publish some code, every time you commit some code to a branch, then you can get some continuous integration style thing. There are also things like, also "Queue" triggers, so you can have an Azure Service queuing, a message, and then, every time there is a message added to the queue, then the function is going to run. There is really a whole range of things. Another thing we can specify, we can precise here, is that here we have C Sharp but we actually support a lot of languages. JavaScript is an obvious one but we also supporting like the F Sharp or other. There are even some with Python so if you want to do some data analysis for input, it can be more suited. So here, I have, in that case, C Sharp and because I specified num1 and num2 in the URL in the route like we saw before, those are going to be mapped automatically in my function. And here, what I'm doing, I'm just going to add them. So, it's very, very easy. >> Very complicated Mathematics. >> Exactly. >> We really need Azure. >> Oh, yeah. No, no, this is super easy but it's just to show the principle, really. And here, I'm going to return the response with the addition. And so, what we're going to do now, this is already in the Cloud, right? So that's cool, right? I already went in and I said, "Okay, I'm going to calculate things and now, I can go and get the function URL here." >> And it actually has your route automatically. >> And it has a route automatically, so you have the num1 here, you have the num2 here. So now, I copy that. I go into another portal, into another tab here. And if I can just go and replay. So, let's see one, two and then the second one I'm going to see is three, four. And now, this URL is pretty ugly because there is a code. But of course, you can minify it to make it beautiful. And now, I'm going to execute that. And after a short wait, we should see something in the work. No, it did actually work but for some reason, because, I'm in Chrome, it shows me the XML. In normal cases, you don't really have to worry about it but here, it tells me that the result is 46 which is what you expect. So, that's cool. But since we have an URL, we can really use that into a Xamarin application which is kind of the point, right? And so here, if I go into my exam calculator and now, this is in Xamarin forms application, and here, I have two entries, number one, number two. I have an Add button and I have a label for the result, okay? And if I go to my code, here, I have the URL that we saw before. So, this is a famous URL which is pretty ugly but it's going to be hidden. The user will never see that. I have an HTTP client which is what we used to call and then after that, I have my button, I'm going to parse the number one and the number two, okay? And eventually, I'm going to replace number one with the first operand, number two with the second operand. I'm going to call my client, GetStringAssync. I get the string back because it sees all HTTP base and then, I'm going to set that on the result. And so, if we go into Xamarin. So in that case, I'm running the Android version but of course, this is in Xamarin form so I also have an iPhone version for free, I have a UWP Windows version for free as well, and I can go and change one, two. I'm going to change three, four, and if I add, after a short wait, we should see, again 46. >> Nice. >> So that works. So, super simple demo here. >> Like the 051. >> Yeah. >> When I do that 101, now I do 051. But this makes sense to me because at this point, it seems like there is clear inputs and outputs. >> Correct. >> Inputs happen to be numbers. >> Correct. >> But they could be an image, it could be processing the image like you said. It could be a clutter of things which is really cool. So, real simplistic but, sometimes, I like to go back to the basics. Sometimes, you see this damage, I'm going to do this and then, do this and there's 8,000. But how does it work, right? >> And really, the point of these demos, this is a demo, by the way, which is going to be published on GitHub with a whole article and we going to do a little bit of promotion on that because we know that we have, in Xamarin, we have a lot of people who come from iOS, who come from Android, and they don't even know how to do an HTTP client call. And so here, now, they know how to do that, right? So, really, back to the basics. But I think the more interesting demo is another one which is a little bit bigger and this is what I call my CoinValue demo. And so, this CoinValue has two functions here. So, the first function is called a "Saver" and as you see, it's a little bit different. So here, I'm in Visual Studio. >> What's happening here? >> Exactly. So here, what I did, I went into "File," "New Project." And with "File," "New Project," I did select "Under Cloud," right? So, installed the Cloud Workload and inside the cloud, I selected a function application directly in Visual Studio. So, I didn't going into the portal at all. At this point, I just did everything into Visual Studio and then inside here, you have the Azure Function template, right? And then after that, I went ahead and I said, "Okay, now, I want to create an actual function." So, you do "Add New Item," right? And under "Add New Item," you will get, if I search here for function, here we go, we have Azure function. I can give it a name, I can say "Add" and then it's going to give me a choice of the various triggers. >> So, these are the things that we're talking about. We talked a little bit about the Timer, the Blob. >> Correct. >> There's a lot of them here. >> There are quite a lot of them and some of them can be also chosen with different languages. So, yes. That's pretty good. So here, we have the HTTP Triggers. That's what we did before. The first ones that I'm showing here in Visual Studio is actually a "Timer" trigger. And so, we have this kind of weird expression here. This is called a Cron Expression, and literally, it's going to say, "Okay, every hour, every minute, or maybe every hour when the minute is five, for example, so it's 1:05, 2:05, 3:05, then, the function is going to run." So, you can specify the schedule here and all of that is going to translate into some attributes. So here, I have this "Timer" trigger which is going to run, every time's the second is zero, every time the minute is zero, every one hour. So, every one hour on top of the hour, every day, every month, every year. And then, I'm going to do some things here. So, what am I doing here? Well, first of all, I'm going to connect to an Azure Table Storage Account and Azure Tables are a way to store some data in Azure in a very, very easy location. So, we do have Cosmos DB. Cosmos DB is like on steroids, right? It's very solid. You have replication and you have a lot of things. I know you talked about that. And here, we have Azure Tables which is like the super lightweight solution. So, it's great for certain use cases when you want to have some tables but you don't want to have a lot of infrastructure, right? >> Yeah. That makes sense. >> And so, here, I am inside the Azure Storage Explorer. It's a very convenient tool that allows me to look to my subscription, see all my tables, and I do have a table down there somewhere called Coins. And as you see, I have a lot of values already inside there. And really, what I'm storing here is every hour, my function is going to go and get the value of Bitcoin and Ethereum, so it's a cryptocurrency, from a website, from a web service, right? And then storing that into the table. And then, I will have another function which is allowing me to get some analytics on those values. >> So, what you're saying then is, when I think about my current value, like normally then, I would have my app, it would make a RESTful service call, it would get the current method. >> Correct. >> But in that instance, if I have a million people doing that and making a million web requests on an API. So basically, you're just saying like, hey, I'll make the web requests of the server. >> Correct. >> I'll cache the coins. >> So, I guess I'm not overloading their service. It's a self-party service. It's free, thank you, guys, right? Like these, I'm doing things. Also, it allows me to have a number of values, and so I can do some analytics on that like here, in that case, I'm going to calculate the trend, right? Is Bitcoin going up, flat, or down, and that's going to be interesting. So, in order to save that, basically, what the function is doing, it's going to connect, and again, this is published sample, so you can go in GitHub and get it, right? So, I'm going to connect to my table, the table I just showed you. If it doesn't exist, I'm going to create it so that's very convenient. If I want to delete it for some reason, it's just going to be recreated automatically. And then, I'm going to connect to this here, current market gap, but basically, it's a free service which returns me some JSON who is also value of old cryptocurrencies, basically. >> Oh, nice. >> And so, I'm going to do some parsing. I'm not going to do the parsing because parsing is always annoying but I'm going to parse for Ethereum and for Bitcoin. >> Got it. >> And eventually, I'm going to create some entities down there. And so, I create one entity for Bitcoin. Entity is just some things that you can store in an Azure Table. Azure Table is very versatile. They take pretty much any object and they are going to automatically create the columns. >> Got it. >> And I'm going to save the value of Bitcoin. I'm going to save, do exactly the same thing for the value of Ethereum. In addition, in this sample, it's kind of a beefy example. It's a little bit bigger. I'm also going to send some push notifications to the UICs. So if you do have the application, the Xamarin application running on your device, you also get, every hour, a notification saying, "Hey, the price of Bitcoin is so much, the price of Ethereum is so much." >> So, this way, when you're actually processing the current value, the user's subscribed to push notifications, it magically happens. >> I'm also going to send that here. >> So you don't have to run like another service deal. You're just doing it right inside. >> I'm just doing it right here and this is running into Azure and by the way, the notification here is sent through the Visual Studio App Center. >> Oh, cool. >> Great way to send notifications. I don't want to go deep into that because it's kind of another topic for itself. >> We'll do another Xamarin Show on this. >> Exactly. >> I totally set it up and I was like, the API is beautiful. >> Yeah, it's really lovely. So basically, I'm just going to send a notification. This is going to run every hour and the cool thing is that you can actually debug that directly in Visual Studio. So before, I showed you the function was running into the Azure Web Portal which makes testing and debugging a little bit tricky, right? I mean, you can do logs and all that, but here, I can literally put a break point here and I can make sure that this function application is selected as Delta Project, and then you can run it in. >> Just in any other council application. >> Any other application. And the nice thing here is that, what starts here is actually is a run time. It is the Azure Function run time. In certain emulation, it is the actual run time. The same run time that is running on Azure is going to run here. So, you'll have a console here and I'll throw a short way. It's going to start. It's going to jump into the debugger and you're going to be able, well in that case, it's not set up properly. My machine broke, you know that, so it's another machine but you can do that. It will jump in the break point and you can step through it and do everything. You can even test also the HTTP Trigger Functions, so when you start the run time, it's going to give you a URL, a local host URL. >> Got it. >> Connect to that, test it. It's really beautiful. So, this is the first function. The other function was the HTTP Trigger. >> So this function, this LbCoinValue, it's a collection of functions? Or they're functions in functions? >> So, you have Function application. So, Functions application is an application with multiple functions. So, you can build a whole API. It doesn't have to be like that, it's just more like a logical grouping. But the good thing is that when you publish, basically, you publish the application, it's going to automatically publish all the functions in one go. >> It makes sense. >> So, here, in that case, I have two functions. One application with two functions. >> Got it, okay. >> And so, the other function is here and this time it's going to be an HTTP trigger, okay? And here again, I have the root. So here, I'm going to say Get and I'm going. >> This is nice. This is like all in line. There's no other files. That's cool. >> Everything is in line. Everything is configured through attributes. >> That's beautiful. >> So, I'm going to say, this symbol is going to be, and then again, before we had num1, num2. Here, I have symbols so I can get BTC or ETH for Ethereum. And then same thing, I'm going to connect to my table. So, this is pretty much a similar code. I'm going to do a query on those tables, okay? I'm going to calculate a little bit of analytics here. So basically, I'm just calculating the regression. So is it going up, flat or down over the 10 plus hours? And then eventually, I'm returning that. Then, you see here, the big difference with before is that I'm returning that as application/json. And so, the function is going to automatically serialize my object which, it's a coding trend, basically. It's going to serialize that to JSON. And that's going to automatically run here. >> Oh, very cool. >> Yeah. And so, after that. >> A little bit more complex. >> Slightly more complex but the big advantage of having JSON is that you can easily add some properties. If you want to have items, a diamond, a server, or whatever, the weather. So after that, we have here an Android, there is an iOS and there is a UWP Windows Application that was also under the Xamarin form, so these are full-blown Xamarin, so-called, classic, right? So, you can do the whole design into Visual Studio directly if you want, storyboard design for iOS, et cetera. And this is actually an MVVM Light Application so it's a full blown MVVM structured. So somewhere here, I have my model. I have my service. And as you see, same thing as before, I have a URL, okay? Somewhere in this URL, I have the symbol that I need to replace. And then I'm going to use any G2B client to do the call, and then I'm going to de-serialize the JSON using JSON.NET. >> Got it. >> And that's really that easy. I mean, literally four lines of code to call the service. >> And that way, your app is not calling, parsing. In fact, it published your app faster because it doesn't have to do any of the trend, any math if you were to query a database. That's all done on these servers which are going to be way faster than your mobile device. >> Yeah, and we could optimize that even more. I mean, the trend is obviously not going to change because since I save every hour, we could say, okay, if the trend wasn't calculated yet, I could save the value and discount it only after an hour, et cetera. >> And then, we could also update the logic. I think, that's what's cool, right? If you want to do Bitcoin, you want to save every 30 minutes, you don't have to change your app. >> No. >> It just works. >> Totally. And so here, this is a client. So as you see, we have an arrow. We have the value of Bitcoin, value of Ethereum. I'm going to connect and again, the same sample runs into iOS and into UWP as well. And since I'm also sending notifications, so here, we have a little bit slower because of the start. But basically, now the function is running, so we see the value is going down slightly but it's so high that it's not surprising. >> I made two different web requests, you asked for Bitcoin first and then Ethereum. Can you ask for Deutsche coin, for instance? It's my favorite coin to collect, Deutsche coin. I highly recommend. Don't take financial advice, though. I've no coins. Zero coins. >> I think, the best advice is only invest what you can afford to lose because we never know. But literally, I mean, you can configures that very easily. In that case, I do two requests, I could do only one request having a bigger JSON payload. It doesn't matter. You can configure that as you want. And really, the key of the issue here is that I don't have to worry about having a web API service. I don't have to worry about, oh my God, it's going to upgrade the operating system or to upgrade this with that version, whatever. In that case, I'm running this own ASP.NET with.Net 461. >> Got it. >> But I could also choose to do it in.NET Core, and I could choose Linux server if I prefer. And you can really configure that as you want. So, in my opinion, it's a great way to build API, especially for mobile applications because it makes it so easy to call them over REST which is really what you want these days, right? >> And then, once you have these values, you could store it however you want to. That's super cool. I like the transition, like this is the getting up and running, this is the real world example of how you would use it. And it's really cool that you can just do it all in Visual Studio or VS for Mac because you can just debug locally and see it. >> In Visual Studio for Mac or in Visual Studio for Windows, the fact that you can set up your unit, that you can debug just like you would any other, and by the way, in this application, there are also some unit test. So, if you go and get the value from GitHub, and I guess we'll publish the link into the Show Note. So down here, I also have a unit test. So, I'm doing some unit test on my view model just the way that you would do a normal thing. You can also configure some dummy services, if you want to do some design time or if you want to do some test time debugging, and if you want to have the whole Shebang, then you just connect to the real service and you're good to go then. >> I like that because now it makes kind of my view model, it makes it a little bit dumb. It's like, let's say, I'm just going to call something that I usually have these services, but now, like the service is super thin. So, the thinner, the faster it makes its response of that. >> Totally. >> Awesome. This has been awesome. I love it. >> All right. >> I saw this demo and now I have a push notification. This is so cool. Well, thank you for coming on talking about some Azure functions. >> Absolutely. Pleasure. >> Hopefully, we'll have you back on again in the future to see what's going on with MVVM Light. I'm excited. >> Yeah, always happy to talk about that. >> So, thanks for coming on. Thanks for tuning in to yet another episode of The Xamarin Show. Don't forget to subscribe. That button's up over there, down over there, over there. Ding that bell. You know what to do. Subscribe so you get the latest notifications of The Xamarin Show right in your inbox and in your notification feed. So, until next time, I'm James Montemagno and thanks for watching.
Info
Channel: Microsoft Developer
Views: 1,629
Rating: undefined out of 5
Keywords: channel9, azure, azure functions, serverless, xamarin, ios, android, bitcoin, xamarin.forms, functions, c#
Id: boHsw0Xvr2U
Channel Id: undefined
Length: 29min 15sec (1755 seconds)
Published: Thu Feb 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.