<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nehansh Jain</title>
    <description>The latest articles on DEV Community by Nehansh Jain (@nehanshj).</description>
    <link>https://dev.to/nehanshj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F725294%2F7b9f6f59-3511-4f07-ba21-6ae3a8adb3d8.jpeg</url>
      <title>DEV Community: Nehansh Jain</title>
      <link>https://dev.to/nehanshj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nehanshj"/>
    <language>en</language>
    <item>
      <title>Cloud Functions with Dart and Appwrite</title>
      <dc:creator>Nehansh Jain</dc:creator>
      <pubDate>Sun, 17 Oct 2021 17:01:50 +0000</pubDate>
      <link>https://dev.to/nehanshj/cloud-functions-with-dart-and-appwrite-2nob</link>
      <guid>https://dev.to/nehanshj/cloud-functions-with-dart-and-appwrite-2nob</guid>
      <description>&lt;p&gt;Being a Flutter Developer it becomes hard to use cloud functions as they need to be written in Javascript/Node.js. Appwrite came as a surprise to me.&lt;/p&gt;

&lt;h5&gt;
  
  
  What is Appwrite?
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://appwrite.io" rel="noopener noreferrer"&gt;Appwrite&lt;/a&gt; is an open-source, self-hosted Backend-as-a-Service that makes app development easier with a suite of SDKs and APIs to accelerate app development.&lt;/p&gt;

&lt;h4&gt;
  
  
  Where is stands against Firebase?
&lt;/h4&gt;

&lt;p&gt;It is the go-to open-source alternative to Firebase. It gives you all what Firebase has to offer including Cloud Functions. That brings us to our topic of discussion.&lt;/p&gt;

&lt;h4&gt;
  
  
  What are Cloud Functions?
&lt;/h4&gt;

&lt;p&gt;As per &lt;a href="https://cloud.google.com/functions/docs/concepts/overview" rel="noopener noreferrer"&gt;Google&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cloud Functions lets you write simple, single-purpose functions that are attached to events emitted from your cloud infrastructure and services. Your function is triggered when an event being watched is fired. Your code executes in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;But most platforms don't allow to write functions in dart or need to be trans compiled to JavaScript&lt;/strong&gt; making it a tedious process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Appwrite comes to our Rescue. It provides Dart runtime upto Dart v2.13&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Appwrite can  also execute our custom code in response to any Appwrite system event like account creation, user login, or document update.&lt;/p&gt;

&lt;p&gt;So, let's first install Appwrite on our Systems.&lt;/p&gt;

&lt;h5&gt;
  
  
  Docker Installation
&lt;/h5&gt;

&lt;p&gt;You can download the executable file to install Docker for Windows from &lt;a href="https://docs.docker.com/desktop/windows/install/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and follow the easy steps. After Successfull installation you'll be left with a screen like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnj4mhzv7hm24lwykr4y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnj4mhzv7hm24lwykr4y.png" alt="Docker Installed"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h5&gt;
  
  
  Appwrite &lt;a href="https://appwrite.io/docs/installation" rel="noopener noreferrer"&gt;Installation&lt;/a&gt;
&lt;/h5&gt;

&lt;p&gt;Just choose a directory to be the root of your appwrite installation. and run the following command on PowerShell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --rm ,
    --volume /var/run/docker.sock:/var/run/docker.sock ,
    --volume ${pwd}/appwrite:/usr/src/code/appwrite:rw ,
    --entrypoint="install" ,
    appwrite/appwrite:0.10.4

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Enabling Dart Environment
&lt;/h6&gt;

&lt;p&gt;In the Appwrite installation folder look for .env file. Within the file add &lt;code&gt;dart-2.12&lt;/code&gt; to the comma seperated list in the environment variable &lt;code&gt;_APP_FUNCTIONS_ENVS&lt;/code&gt; and &lt;code&gt;_APP_FUNCTIONS_RUNTIMES&lt;/code&gt;. This enables Dart runtime environment for Appwrite Cloud Functions.&lt;/p&gt;

&lt;p&gt;In the root directory of Appwrite where you have your &lt;code&gt;docker-compose.yml&lt;/code&gt; run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that on opening Docker you'd be able to see Appwrite Instance running.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fby1if1uhovb8nneuyrqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fby1if1uhovb8nneuyrqk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check if everything is ok you can try running &lt;code&gt;docker exec appwrite doctor&lt;/code&gt; which will give you a similar output:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnf81hfd74vdicugylsy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnf81hfd74vdicugylsy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to your defined host address and you'll be able to see the appwrite console. You'll need to create a new User to access the console. The Landing page after signing in would look something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ylh51zyp7ddic00wjk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ylh51zyp7ddic00wjk1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Project Creation
&lt;/h4&gt;

&lt;p&gt;Create a new folder  with &lt;code&gt;main.dart&lt;/code&gt; and &lt;code&gt;pubspec.yaml&lt;/code&gt;, or you can copy from Appwrite Demos Function &lt;a href="https://github.com/appwrite/demos-for-functions/tree/master/dart/" rel="noopener noreferrer"&gt;Repository&lt;/a&gt;.&lt;/p&gt;
&lt;h6&gt;
  
  
  Adding Dependency
&lt;/h6&gt;

&lt;p&gt;We need to install the dart_appwrite package present on &lt;a href="https://pub.dev/packages/dart_appwrite" rel="noopener noreferrer"&gt;pub.dev&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;open your &lt;code&gt;pubspec.yaml&lt;/code&gt; file and make sure it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: project_name
version: 1.0.0 
description: ""

environment:
  sdk: "&amp;gt;=2.12.0 &amp;lt;3.0.0"

dependencies:
  dart_appwrite: ^1.0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Template code for your Cloud Function
&lt;/h6&gt;

&lt;p&gt;add the following code in your &lt;code&gt;main.dart&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'dart:io';

import 'package:dart_appwrite/dart_appwrite.dart';

void main(List&amp;lt;String&amp;gt; args) {
  // Initialise the client SDK
  final Client client = Client();
  client
          .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint
          .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID']) // Your project ID
          .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key
      ;

//Your Custom Code
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use your system's local ip address instead of localhost or 127.0.0.1.&lt;/p&gt;

&lt;h6&gt;
  
  
  Getting Dependencies
&lt;/h6&gt;

&lt;p&gt;In your Terminal, run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ set PUB_CACHE=.appwrite
$ dart pub get
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will get dependencies and store them inside &lt;code&gt;.appwrite&lt;/code&gt; folder. Now you project will have 2 auto-created files as &lt;code&gt;.appwrite&lt;/code&gt; and &lt;code&gt;pubspec.lock&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Deploying Cloud Function
&lt;/h4&gt;

&lt;p&gt;Inside the Appwrite Console, tap on the &lt;strong&gt;Functions&lt;/strong&gt; menu. In the functions dashboard, tap the &lt;strong&gt;Add Function&lt;/strong&gt; button.&lt;/p&gt;

&lt;p&gt;Inside the Dialog Box enter your function name and choose the environment as &lt;code&gt;Dart 2.12&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mc1wk044uy3u2x8w030.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mc1wk044uy3u2x8w030.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
 Clicking on create will create a function on the appwrite console.&lt;/p&gt;
&lt;h5&gt;
  
  
  Deploy Tag/Custom Dart Code
&lt;/h5&gt;

&lt;p&gt;Before deploying we'll need to create a GZipped File of your project. So in the root directory of your dart project, run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ..
$ tar -zcvf code.tar.gz project_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this go to the Function overview page and click on &lt;em&gt;Deploy Tag&lt;/em&gt;. Write command as &lt;code&gt;dart main.dart&lt;/code&gt; and select your GZipped File.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flaoi3ybqs2vwmoms3lkp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flaoi3ybqs2vwmoms3lkp.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;or you can also deploy the function using CLI by entering the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;appwrite functions createTag `
    --functionId=6162f0965a4e4 `
    --command='mycommand' `
    --code='/myrepo/myfunction'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Deployed Tag will appear like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fp6a9g0vbh01987k3v0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fp6a9g0vbh01987k3v0.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Click &lt;em&gt;Activate&lt;/em&gt; and the Deployed Function would be activated and ready to execute.&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing the Function
&lt;/h4&gt;

&lt;p&gt;This is how your Function Overview Page will look like after performing all the above mentioned steps.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa70sb35hrku2oubsn1ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa70sb35hrku2oubsn1ts.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;em&gt;Execute Now&lt;/em&gt; and wait for few seconds and click &lt;em&gt;View Logs&lt;/em&gt;. If the Function ran successfully you'll get something like this.&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ymiiiya2n97u9ull3gf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ymiiiya2n97u9ull3gf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  THANK YOU
&lt;/h2&gt;

&lt;p&gt;This was my take on creating and deploying Cloud Functions with our very own Dart Language. Would love to hear your suggestions and feedback.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>cloud</category>
      <category>docker</category>
      <category>appwrite</category>
    </item>
  </channel>
</rss>
