ANATOLY PULYAEVSKIY: Right.
Hi, everyone.
My name is Anatoly.
And just a fun fact--
I just double-checked the list of all the talks in this
conference, and it looks like this one is the only one that
has "server-side" in the title.
[APPLAUSE]
Just a precious five minutes for server-side.
So this is about server-side Dart on NodeJS,
or how people in Portland like to say it,
putting a bird on it.
[LAUGHTER]
So first of all, why NodeJS?
Why not Dart VM?
And as much as I like Dart VM, the only place
I can be sure it's always installed on is on my laptop.
That's not really anywhere yet.
And on the contrary, with NodeJS,
you can actually see it very much everywhere.
For instance, in Firebase, there is a good chance
that some of you are going to build a Flutter or web
app that uses Firebase.
And in Firebase, they have this service
called Firebase Functions.
Have any one of you used it?
All right, quite a few.
So this is basically a way to run your JavaScript
in the cloud in a JavaScript function.
So why not try this function in Dart,
and compile it in JavaScript, and run it in the cloud?
Some other interesting reasons would be code sharing,
as was mentioned many times before me,
and cohering a development environment.
So what it means how to build a Dart application for NodeJS?
So it's basically very much, how would you build it for web?
It's basically, you need some Dart code.
Then you just add some more Dart code
in the form of JS bindings.
And then you just compile it with Dart2js
and get a JavaScript file.
And the only one missing bit here
is you just need a special header, node_preamble.
This is not my creation, by the way.
This package has been around for a while.
You just prepend a special snippet
in front of this JS file, and it just works in Node.
So what's possible?
I have two demos today.
One is the Firebase Cloud Functions.
And I just need to switch to my code editor here.
So this is an example HTTP Firebase cloud function.
Can you see this code, by the way?
Whoops.
Is it better?
AUDIENCE: Yeah.
ANATOLY PULYAEVSKIY: OK, so I have a date
function here, which is an HTTP function that
accepts incoming HTTP requests and replies with some response
to it.
And the way you register it is by using this built-in Firebase
functions object.
You set a key on it with your function.
Now, what you can notice here is basically
there is not much about Node here.
That's my preference.
I don't like having require calls in my Dart code.
It's more like idiomatic Dart.
And it actually works.
I'm not going to go through the deploy process
here because it takes a while.
But I have this link here.
This is this same function deployed to the cloud.
It might take a while first time to load.
But this is it.
And I'm refreshing it, and it just shows me the date.
I mean, this is an intentionally very basic example.
You can do quite a bit more than that.
You can store that data in real-time database,
in Fire Store, send push notifications, whatever
Firebase services you want to use.
The other example is not about Firebase.
It's just I wanted to try and explore
what else can we do with this combination with Dart
and NodeJS.
And here's an example script I have here.
This is a very basic HTTP server which is powered by Dart VM.
Right?
We bind to a host and port, and we listen for requests,
and we just return some--
write some responses.
And actually, here, I'm using the HTTP package
to call this same date function in the cloud that I showed you.
And it brings out the result.
So I can actually run this code.
And it shows me-- this is Dart VM.
No surprises here.
But what if we can do this, something like this?
We can just replace Dart with Node here?
Obviously, we cannot do this here,
but I can try and do it a little bit differently.
I just want to replace Dart VM I/O with Node I/O. And here
it would be the same--
like this.
Now we just need to compile it with Pub.
This is basically the same Dart2js compilation.
All right, so now I'm using Node to run
the same application that was using the Dart I/O before.
And as you see, it works.
It basically uses the same interfaces.
[APPLAUSE]
Thanks.
So we can really get a very Darty feel
when working with NodeJS.
That's what I wanted to show you.
Thanks.
[APPLAUSE]