<?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: Faisal Salif</title>
    <description>The latest articles on DEV Community by Faisal Salif (@dekin0linux).</description>
    <link>https://dev.to/dekin0linux</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%2F1217903%2Feeadcaf3-35a7-4997-8f30-a4f7c849bc70.png</url>
      <title>DEV Community: Faisal Salif</title>
      <link>https://dev.to/dekin0linux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dekin0linux"/>
    <language>en</language>
    <item>
      <title>HOW TO SET UP AND USE gRPC IN REACT OR NEXT</title>
      <dc:creator>Faisal Salif</dc:creator>
      <pubDate>Fri, 24 Nov 2023 11:31:55 +0000</pubDate>
      <link>https://dev.to/dekin0linux/setting-grpc-in-reactnext-js-1m6e</link>
      <guid>https://dev.to/dekin0linux/setting-grpc-in-reactnext-js-1m6e</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Download and install the protoc complier file&lt;br&gt;
&lt;a href="https://github.com/protocolbuffers/protobuf/releases/tag/v25"&gt;https://github.com/protocolbuffers/protobuf/releases/tag/v25&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install protoc packages globally&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm install -g protoc-gen-js protoc-gen-grpc-web&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install these two packages in your local project&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm install grpc-web google-protobuf&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;strong&gt;proto.sh&lt;/strong&gt; file in Root Dir of your project, (this will have the script to compile our raw protos to js or tsx&lt;br&gt;
=&amp;gt; Sample of the script i used&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d1PleFhN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjr26i4xvuorlrg7zyiu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d1PleFhN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjr26i4xvuorlrg7zyiu.png" alt="SHELL SCRIPT" width="800" height="634"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Add a script property in you packge.json to run the shell file&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;example  "proto-build":"sh path_to_shell_file"&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a folder called protos in your src or app folder and inside the protos folder create a folder called raw. which we'll keep all our raw proto files. e.g &lt;strong&gt;src/proto/raw&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;run the script to compile raw proto file = &lt;strong&gt;npm run proto-build&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;=&amp;gt; CREATING API FROM COMPLIED PROTOS&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a folder in your src and name it api , this folder will have all api files we would be using for our api calls.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a apiConfig.js file in your src/app directory , this file will have the URL and METADATA (configurations)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;e.g export const URL = "url_to_your_backend"&lt;br&gt;
     export const METADATA = {&lt;br&gt;
        key: "key_to_connect", &lt;br&gt;
        pass:"password_to_connect" &lt;br&gt;
    }&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now we can go ahead to connect to the backend and consume our api's.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JiS42zu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyi1bdl2by0iou7vlfts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JiS42zu8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyi1bdl2by0iou7vlfts.png" alt="Image description" width="800" height="743"&gt;&lt;/a&gt;&lt;br&gt;
This how I connect to my api and access a method from the proto services&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import {METADATA,URl} from './app/apiConfig.js'&lt;/li&gt;
&lt;li&gt;import {CustomerSvcClient} from './protos/gen/Customer_grpc_web_pb' (give you the service method you need to use)&lt;/li&gt;
&lt;li&gt;import {Request1,Request2,..} from './protos/gen/Customer/pb' (gives you the messages or requests to you need to use)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    function CustomerController(){
       const client = new CustomerSvcCLient(URL,null,null)

       //Creating a function to make your api requests
       const getAllCustomers = async(**data**)=&amp;gt;{
       try{
         return new Promise(resolve,reject)=&amp;gt;{
         const request = new Request1()

         //if our request requires a body we can attach them as so
         request.setName(data.name)
         request.setAge(data.age)

         //We can make out api call by using a method from our 
         client 
         client.getAllUsers(request,METADATA,(err,response)=&amp;gt;{
           if(err) reject(err)
           //converts our response to object
           const result = response.toObject() 
           resolve(result)
         })
       }
      }
     }catch(err){
      console.log(err)
     }

     cosnt anotheFucntion=async()=&amp;gt;{....}



    return {getAllCustomers,anotherFunction}



    }

export default CustomerController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Consuming the api and displaying data e.g App.js
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import CustomerController from './file_directory'

//grab the methods you want to use from the API file
const {getAllCustomers,anotherFunction} = CustomerController() 

  useEffect(()=&amp;gt;{
     async function getData(){
       const getValue = await getAllCustomers({name:'myname',age:20})
       console.log(getValue.userlistList)
       setData(getValue.userlistList)
     }
     getData()
   },[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>grpc</category>
      <category>protoc</category>
      <category>api</category>
      <category>google</category>
    </item>
  </channel>
</rss>
