<?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: Taher Fattahi Tabalvandan</title>
    <description>The latest articles on DEV Community by Taher Fattahi Tabalvandan (@taherfattahi).</description>
    <link>https://dev.to/taherfattahi</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%2F374968%2F15d93416-62ba-404f-8e0e-0a9a5e8fcd20.jpg</url>
      <title>DEV Community: Taher Fattahi Tabalvandan</title>
      <link>https://dev.to/taherfattahi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/taherfattahi"/>
    <language>en</language>
    <item>
      <title>Markov Decision Processes(MDP) basic concept</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Thu, 23 Nov 2023 13:31:46 +0000</pubDate>
      <link>https://dev.to/taherfattahi/markov-decision-processesmdp-501j</link>
      <guid>https://dev.to/taherfattahi/markov-decision-processesmdp-501j</guid>
      <description>&lt;p&gt;A Markov decision process (MDP) is a mathematical framework used to model decision-making problems in which outcomes are partly random and partly under the control of a decision-maker. It consists of a set of states, a set of actions, and a set of rewards. The decision-maker takes actions in each state, and the environment transitions to a new state based on the action taken and a probability distribution over the next state. The decision-maker receives a reward based on the state and action taken.&lt;/p&gt;

&lt;p&gt;Here’s an example of an MDP:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Consider a robot that is trying to navigate through a maze. The robot can move in four directions: up, down, left, or right. The robot’s goal is to reach the exit of the maze. The robot’s current state is its current location in the maze. The robot can only see a limited area around it, so it doesn’t have access to the entire maze. The robot’s actions are to move in one of the four directions. The environment transitions to a new state based on the action taken and a probability distribution over the next state. For example, if the robot moves up, there might be a 70% chance that it stays in the same location, a 10% chance that it moves to the left, a 10% chance that it moves to the right, and a 10% chance that it moves down. The robot receives a reward based on the state and action taken. For example, the robot might receive a reward of +10 if it reaches the exit of the maze, a reward of -1 if it bumps into a wall, and a reward of -0.1 for each step it takes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The environment is typically formulated as a Markov Decision Process&lt;/p&gt;

&lt;p&gt;1.) set of states: S&lt;br&gt;
In chess, a state is a given configuration of the board&lt;/p&gt;

&lt;p&gt;2.) set of actions: A (for example: up, down, left, right)&lt;br&gt;
Every possible move in chess or tic-tac-toe&lt;/p&gt;

&lt;p&gt;3.) Conditional distribution of the next state&lt;br&gt;
The next state depends on the actual one exclusively „Markov-property"&lt;br&gt;
P(s'|s,a) transition probability matrix&lt;/p&gt;

&lt;p&gt;4.) R(s,s') the reward function of the next s' state after the agent makes action a while being in the state s&lt;/p&gt;

&lt;p&gt;5.) γ gamma discount factor&lt;/p&gt;

&lt;p&gt;The Markov property is a fundamental assumption in reinforcement learning. It states that the current state of an agent contains all the information that is relevant for decision-making. In other words, the future state of the agent depends only on its current state and not on its past states. This assumption enables theoretical proofs for certain algorithms and is widely used in current research.&lt;/p&gt;

&lt;p&gt;For example, in a Markov decision process (MDP), we assume that the current state is independent of the whole history given the previous state. However, in real-life scenarios, this assumption may not always hold true and can lead to an RL algorithm failing in a specific environment.&lt;/p&gt;

&lt;p&gt;Reference:&lt;br&gt;
&lt;a href="https://web.stanford.edu/class/cme241/lecture_slides/david_silver_slides/MDP.pdf" rel="noopener noreferrer"&gt;https://web.stanford.edu/class/cme241/lecture_slides/david_silver_slides/MDP.pdf&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>reinforcementlearning</category>
    </item>
    <item>
      <title>FluffyChat push notification with sygnal!!</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Thu, 04 Aug 2022 07:30:00 +0000</pubDate>
      <link>https://dev.to/taherfattahi/fluffychat-messenger-in-your-application-4f3b</link>
      <guid>https://dev.to/taherfattahi/fluffychat-messenger-in-your-application-4f3b</guid>
      <description>&lt;p&gt;How you can add &lt;a href="https://fluffychat.im/" rel="noopener noreferrer"&gt;FluffyChat&lt;/a&gt; messenger to your custom application with your package name or your unique bundle id?&lt;/p&gt;

&lt;p&gt;FluffyChat source code =&amp;gt; &lt;a href="https://gitlab.com/famedly/fluffychat" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my new project development, I realized some information about FluffyChat Messenger.&lt;/p&gt;

&lt;p&gt;You can add all of the FluffyChat source code to your new project but absolutely your push notification not working,&lt;br&gt;
if you want to use Firebase Cloud Messaging in FluffyChat for sending a push notification in your new application.&lt;/p&gt;

&lt;p&gt;Plz follow these steps:&lt;/p&gt;

&lt;p&gt;1) Create new app in &lt;a href="https://console.firebase.google.com/" rel="noopener noreferrer"&gt;https://console.firebase.google.com/&lt;/a&gt; and add &lt;code&gt;google-serices.json&lt;/code&gt; to your project&lt;/p&gt;

&lt;p&gt;2) Setup and install &lt;a href="https://github.com/matrix-org/sygnal" rel="noopener noreferrer"&gt;sygnal&lt;/a&gt; in your server&lt;/p&gt;

&lt;p&gt;3) In FluffyChat source code plz change &lt;strong&gt;pushNotificationsAppId&lt;/strong&gt; and &lt;strong&gt;pushNotificationsGatewayUrl&lt;/strong&gt; in &lt;code&gt;app_config.dart&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;4) Add this line of code in sygnal source code:&lt;br&gt;
in &lt;code&gt;gcmpushkin.py&lt;/code&gt; add bottom line of code to &lt;code&gt;_build_data&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data["devices"] = [{"pushkey": device.pushkey,"data": device.data,"pushkey_ts": device.pushkey_ts,"app_id": device.app_id}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see this repository:&lt;br&gt;
&lt;a href="https://github.com/taherfattahi/sygnal" rel="noopener noreferrer"&gt;https://github.com/taherfattahi/sygnal&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5) Add FCM Server key to &lt;code&gt;sygnal.yaml&lt;/code&gt; in sygnal project&lt;/p&gt;

&lt;p&gt;If you have any problem you can get in touch with me, my email address: &lt;a href="mailto:taherfattahi11@gmail.com"&gt;taherfattahi11@gmail.com&lt;/a&gt; 😉&lt;/p&gt;

</description>
      <category>fluffychat</category>
      <category>flutter</category>
      <category>messenger</category>
      <category>matrix</category>
    </item>
    <item>
      <title>Build a Chat service using GoLang and WebAssembly (part 4)</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Sat, 30 Jul 2022 15:32:00 +0000</pubDate>
      <link>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-4-5bka</link>
      <guid>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-4-5bka</guid>
      <description>&lt;p&gt;ok, for communicating with server we need a Websocket Server - i prefer we develop our WebSocket server-side with nodejs.&lt;/p&gt;

&lt;p&gt;our source code =&amp;gt; &lt;a href="https://github.com/taherfattahi/golangChatWebAssembly" rel="noopener noreferrer"&gt;Github Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is WebSocket?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Websocket&lt;/strong&gt; is a simple to use, fast and tested WebSocket client and server implementation.&lt;/p&gt;

&lt;p&gt;The Websocket specification defines an API establishing a connection between a web browser and server. WebSocket is used for creating real-time games, chat applications, displaying stock data, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NodeJS and WebSocket&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;NodeJS&lt;/strong&gt; is a &lt;strong&gt;JavaScript&lt;/strong&gt; runtime built on &lt;strong&gt;Chrome’s V8 JavaScript engine&lt;/strong&gt;. It is easy to build a &lt;strong&gt;WebSocket&lt;/strong&gt; server implementation with NodeJS.&lt;/p&gt;

&lt;p&gt;Traditional &lt;strong&gt;HTTP&lt;/strong&gt; is dependent on client requests, you have to send your request to the server and then get a response from it but in &lt;strong&gt;WebSocket&lt;/strong&gt;, you can send data to the client from the server directly which we call bidirectionally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let me introduce you to Websockets:&lt;/em&gt;&lt;/strong&gt;  An event-driven, web-friendly alternative to &lt;strong&gt;HTTP&lt;/strong&gt;. Websockets don't require a client request to fetch data from the server every time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building a WebSocket Server With NodeJS&lt;/strong&gt;&lt;br&gt;
As prerequisites, you should have &lt;strong&gt;Nodejs&lt;/strong&gt; and &lt;strong&gt;NPM&lt;/strong&gt; Installed in your system. If you don’t have it, download and install it from here: &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;https://nodejs.org/en/download/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1) Once you are ready with &lt;strong&gt;NodeJs&lt;/strong&gt;, open a terminal and type 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;mkdir webSocketServer &amp;amp;&amp;amp; cd webSocketServer 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Create your nodejs project environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Next, run the following command to install the &lt;strong&gt;ws&lt;/strong&gt; library as a dependency:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install ws
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4) Create a javascript file for example “webSocketServer.js” and paste the following code for creating a web server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Importing the required modules
const WebSocketServer = require('ws');

// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: 8080 })

// Creating connection using websocket
wss.on("connection", ws =&amp;gt; {
    console.log("new client connected");
    // sending message
    ws.on("message", data =&amp;gt; {
        console.log(`Client has sent us: ${data}`)
    });
    // handling what to do when clients disconnects from server
    ws.on("close", () =&amp;gt; {
        console.log("the client has connected");
    });
    // handling client connection error
    ws.onerror = function () {
        console.log("Some Error occurred")
    }
});
console.log("The WebSocket server is running on port 8080");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will create a basic WebSocket Server for you and you can provide any port you want while creating the WebSocket server.&lt;/p&gt;

&lt;p&gt;5) For testing it, open up a terminal and type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node webSocketServer.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reference:&lt;br&gt;
1) &lt;a href="https://www.piesocket.com/blog/nodejs-websocket#:~:text=What%20is%20WebSocket%3F,%2C%20displaying%20stock%20data%2C%20etc." rel="noopener noreferrer"&gt;piesocket.com&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;In part 5 we are going to write our WebSocket client-side service with Golang WebAssembly and connect to WebSocket nodejs server-side😉&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>go</category>
      <category>chat</category>
      <category>websocket</category>
    </item>
    <item>
      <title>Build a Chat service using GoLang and WebAssembly (part 3)</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Wed, 20 Jul 2022 06:14:16 +0000</pubDate>
      <link>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-3-1kn3</link>
      <guid>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-3-1kn3</guid>
      <description>&lt;p&gt;Gooood :)))) now we are in part 3 - we wanna talk about &lt;strong&gt;Chat Service Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A chat service consists of two major parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat App&lt;/strong&gt; or client part, which is a desktop, web, or smartphone chat application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat Server Engine&lt;/strong&gt; or server part, which is a pool of external servers responsible for the chat operation. This is the place where all the chat magic happens.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both parts contain various components that communicate to each other and bring the chat into action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsrxfylj9uq1h31hs8kih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsrxfylj9uq1h31hs8kih.png" alt="Chat Architecture" width="800" height="462"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chat Server Engine&lt;/strong&gt; is a core of the chat architecture that handles message delivery and dispatch. I want to show you all components in chat service:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat REST API&lt;/strong&gt; handles the tasks that are not connected directly to message dispatch and delivery, such as user authentication, changing of user settings, friends invitation, downloading sticker packs, etc. The Chat App (the chat client part) communicates with the Chat REST API via the Chat REST API Client Library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat WebSocket Server&lt;/strong&gt; is responsible for transmitting messages between users. The Chat App communicates with the Chat WebSocket Server via the Chat WebSocket Client Library. This connection is open two ways; that means users don’t have to make requests to the server if there are any messages for them, they just get them right away.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat Media Storage Server&lt;/strong&gt; is a pool of servers responsible for storing user media files. The files are uploaded to them via the Chat Media Storage Client Library.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Chat App&lt;/strong&gt; is the other major part of the chat architecture, the one that users directly interact with. It's split into three separate root components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat Client Engine&lt;/strong&gt; handles all of the communication with the Chat Server Engine via its internal components: Chat REST API Client Library, Chat WebSocket Client Library and Chat Media Storage Client Library. It also comprises the &lt;strong&gt;Chat Push Message Handler&lt;/strong&gt; that deals with push notifications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat UI&lt;/strong&gt; displays data to users via its widgets: &lt;strong&gt;Chat Contact List UI, Chat Dialog UI, Chat Push Message Widget&lt;/strong&gt; — extension for mobile apps that allow for replying to messages without opening the app and &lt;strong&gt;Chat Internal Notification Widget&lt;/strong&gt; — a widget that pops up at the top of the screen while the user is chatting in a dialog and notifies about the incoming message in another dialog.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Chat Device Storage&lt;/strong&gt; is an internal database (read: your device storage), which stores messages and files so that users can access them offline. Its internal component, &lt;strong&gt;Chat Media Cache&lt;/strong&gt;, gets media files from the Chat Media Storage and stores them on the device so that the user can access them anytime without having to reach the Chat Media Storage every time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we need &lt;strong&gt;WebSocket&lt;/strong&gt; protocol that makes it possible to open a two-way interactive communication session between the user's browser and a server. so you can send messages to a server and receive event-driven responses without having to poll the server for a reply.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9dincge3wb3jtzfeerrk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9dincge3wb3jtzfeerrk.png" alt="Image Websocket" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our project we create a &lt;strong&gt;websocket server with nodejs&lt;/strong&gt; and a &lt;strong&gt;websocket client with Golang&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
1) &lt;a href="https://yellow.systems/blog/guide-to-the-chat-architecture" rel="noopener noreferrer"&gt;yellow.systems: Chat Service Architecture&lt;/a&gt;&lt;br&gt;
2) &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" rel="noopener noreferrer"&gt;developer.mozilla.org: WebSocket&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;In part 4 we are going to write our websocket server with nodejs😉&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>go</category>
      <category>chat</category>
    </item>
    <item>
      <title>Build a Chat service using GoLang and WebAssembly (part 2)</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Fri, 15 Jul 2022 09:58:00 +0000</pubDate>
      <link>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-2-1e4l</link>
      <guid>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-2-1e4l</guid>
      <description>&lt;p&gt;&lt;em&gt;ok, now we want to write our first WebAssembly code with golang&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;this is our source code structure:&lt;br&gt;
&lt;a href="https://media2.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%2Fnzern2qr9gr7hcqlz4fl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fnzern2qr9gr7hcqlz4fl.png" alt="structure" width="188" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;our source code =&amp;gt; &lt;a href="https://github.com/taherfattahi/golangChatWebAssembly" rel="noopener noreferrer"&gt;Github Link&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Hello World WebAssembly Program Cross Compiled from Go:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start by writing a simple hello world program in Go, cross compile it to WebAssembly, and run it on the browser.&lt;/p&gt;
&lt;h3&gt;
  
  
  1) At the first you have to install golang compiler on your device =&amp;gt; &lt;a href="https://go.dev/dl/" rel="noopener noreferrer"&gt;golang&lt;/a&gt;
&lt;/h3&gt;
&lt;h3&gt;
  
  
  2) Creating a directory with a new project:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir golangChatWebAssembly 
cd golangChatWebAssembly 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  3) Let’s create golangChatWebAssembly module:
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go mod init golangChatWebAssembly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  4) Let’s create main.go file
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir app &amp;amp;&amp;amp; cd app
touch main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  5) Let’s fill our application with content
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;app/main.go&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (  
    "fmt"
)

func main() {  
    fmt.Println("Go Web Assembly")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6) Now it’s time to build our app
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;This command will compile our go application into the main file. wasm (with wasm bytecode) is an executable module of a web assembly.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;if you have &lt;strong&gt;Linux&lt;/strong&gt; or &lt;strong&gt;MacOS&lt;/strong&gt; use bottom command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GOOS=js GOARCH=wasm go build -o ./html/main.wasm .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you have &lt;strong&gt;Windows&lt;/strong&gt; OS I suggest creating &lt;strong&gt;build.bat&lt;/strong&gt; file and then running it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch build.bat 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on your &lt;strong&gt;build.bat&lt;/strong&gt; file write this syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET GOOS=js
SET GOARCH=wasm

go build -o ./html/main.wasm .

SET GOOS=windows
SET GOARCH=amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then run it in command line: &lt;strong&gt;.\build.bat&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7) Javascript Glue
&lt;/h3&gt;

&lt;p&gt;As we already discussed (&lt;a href="https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-1-1pee"&gt;part 1&lt;/a&gt;), WebAssembly is supposed to exist hand in hand with JavaScript. Hence some JavaScript glue code is needed to import the WebAssembly Module we just created and run it in the browser. This code is already available in the Go installation. Let's go ahead and copy it to our &lt;strong&gt;html&lt;/strong&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./html/  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command copies the wasm_exec.js that contains the glue code to run WebAssembly into the &lt;strong&gt;html&lt;/strong&gt; directory.&lt;/p&gt;

&lt;p&gt;in &lt;strong&gt;Windows OS&lt;/strong&gt; download &lt;strong&gt;wasm_exec.js&lt;/strong&gt; from this &lt;a href="https://raw.githubusercontent.com/taherfattahi/golangChatWebAssembly/master/app/html/wasm_exec.js" rel="noopener noreferrer"&gt;link &lt;/a&gt;and then copy file into &lt;strong&gt;html&lt;/strong&gt; folder&lt;/p&gt;

&lt;h3&gt;
  
  
  8) we have to create a HTML page with an application call to the Web Assembly
&lt;/h3&gt;

&lt;p&gt;create &lt;strong&gt;index.html&lt;/strong&gt; in &lt;strong&gt;app/html&lt;/strong&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;meta charset="utf-8"/&amp;gt;
        &amp;lt;script src="wasm_exec.js"&amp;gt;&amp;lt;/script&amp;gt;
        &amp;lt;script&amp;gt;
            if (!WebAssembly.instantiateStreaming) {
                WebAssembly.instantiateStreaming = async (resp, importObject) =&amp;gt; {
                    const source = await (await resp).arrayBuffer();
                    return await WebAssembly.instantiate(source, importObject);
                };
            }

            const go = new Go();
            WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then(result =&amp;gt; {
                go.run(result.instance);
            });
        &amp;lt;/script&amp;gt;
    &amp;lt;/head&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9) we will also need a web server
&lt;/h3&gt;

&lt;p&gt;Let’s create a simple web server on Go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ..
mkdir server &amp;amp;&amp;amp; cd server
touch server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;fill our &lt;strong&gt;server.go&lt;/strong&gt; with this source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "log"
    "net/http"
)

const (
    AddSrv       = ":8080"
    TemplatesDir = "../app/html/"
)

func main() {
    fileSrv := http.FileServer(http.Dir(TemplatesDir))

    err := http.ListenAndServe(AddSrv, fileSrv)

    if err != nil {
        log.Fatalln(err)
    }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  10) After that, we start the server
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;server/&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run server.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and open the address in the browser&lt;/p&gt;

&lt;p&gt;&lt;a href="http://127.0.0.1:8080" rel="noopener noreferrer"&gt;http://127.0.0.1:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and in “DevTools &amp;gt; console” you can see the output:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Go Web Assembly&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fyl7m0nplsv0lizku26ul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyl7m0nplsv0lizku26ul.png" alt="Console.log image" width="791" height="27"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;In part 3 we are going to study about chat service architecture 😉&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>go</category>
      <category>chat</category>
    </item>
    <item>
      <title>Build a Chat service using GoLang and WebAssembly (part 1)</title>
      <dc:creator>Taher Fattahi Tabalvandan</dc:creator>
      <pubDate>Thu, 14 Jul 2022 08:23:51 +0000</pubDate>
      <link>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-1-1pee</link>
      <guid>https://dev.to/taherfattahi/build-a-chat-service-using-golang-and-webassembly-part-1-1pee</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fy5vpkf644hmqts18jvsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fy5vpkf644hmqts18jvsz.png" alt="concept 1" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  At the first I want to explain what is Webassembly?
&lt;/h3&gt;

&lt;p&gt;in short, compile and run languages such as java-c#-python,.... into the browser we called Webassembly.&lt;/p&gt;

&lt;p&gt;JavaScript has been the only programming language that the browser understands. JavaScript has stood the test of time and it has been able to deliver the performance needed by most web applications. But when it comes to 3D games, VR, AR, and image editing apps, JavaScript is not quite up to the mark since it is interpreted. Although JavaScript engines such as Gecko and V8 have Just in Time compilation capabilities, JavaScript is not able to provide the high performance required by modern web applications.&lt;/p&gt;

&lt;p&gt;WebAssembly(also known as wasm) is meant to solve this problem. WebAssembly is a virtual assembly language for the browser. When we say virtual, it means that it cannot be run natively on the underlying hardware. Since the browser can be running on any architecture, it is not possible for the browser to run WebAssembly directly on the underlying hardware. But this highly optimized virtual assembly format can be processed much quicker than vanilla JavaScript by modern browsers since it is compiled and is more close to the hardware architecture than JavaScript. The following figure shows where WebAssembly stands in the stack when compared to Javascript. It is closer to the Hardware than JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F050mq3rhuufci6ql1y5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F050mq3rhuufci6ql1y5h.png" alt="WebAssembly" width="121" height="181"&gt;&lt;/a&gt;&lt;br&gt;
The existing JavaScript engines have support to run WebAssembly's Virtual Assembly Code.&lt;/p&gt;

&lt;p&gt;WebAssembly is not meant to replace JavaScript. It is meant to operate hand in hand with JavaScript to take care of performance critical components of a web application. It is possible to make calls from JavaScript to WebAssembly and vice versa.&lt;/p&gt;

&lt;p&gt;WebAssembly is not generally coded by hand, but rather, it is cross compiled from other high level programming languages. For example, it is possible to cross compile Go, C, C++, and Rust code to WebAssembly. Thus the module which has already been coded in some other programming language can be cross compiled to WebAssembly and used in the browser directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Application of WebAssembly (WASM)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Where was WebAssembly supposed to be used at first? First of all, in tasks for processing heavy-duty tasks directly in the browser.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Game development&lt;/li&gt;
&lt;li&gt;Video editing/streaming&lt;/li&gt;
&lt;li&gt;Encryption&lt;/li&gt;
&lt;li&gt;Image recognition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F7hehiij7wl3b9bwr4sel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F7hehiij7wl3b9bwr4sel.png" alt="Concept 2" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;In part 2 we are going to write our first WebAssembly code with golang 😉&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webassembly</category>
      <category>go</category>
      <category>chat</category>
    </item>
  </channel>
</rss>
