<?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: Eslam Heikal</title>
    <description>The latest articles on DEV Community by Eslam Heikal (@heikale).</description>
    <link>https://dev.to/heikale</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%2F910774%2F9d0f2bfe-7481-4a5d-a195-a415d3642124.jpg</url>
      <title>DEV Community: Eslam Heikal</title>
      <link>https://dev.to/heikale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/heikale"/>
    <language>en</language>
    <item>
      <title>Angular Signals</title>
      <dc:creator>Eslam Heikal</dc:creator>
      <pubDate>Sun, 08 Dec 2024 11:31:03 +0000</pubDate>
      <link>https://dev.to/heikale/angular-signals-42ji</link>
      <guid>https://dev.to/heikale/angular-signals-42ji</guid>
      <description>&lt;p&gt;Imagine you’re in a busy café with a friend. Whenever your coffee order is ready, a light above the counter flashes—it’s a signal! You don’t need to keep asking if your coffee is ready; the light tells you instantly.&lt;/p&gt;

&lt;p&gt;They act like messengers, notifying your application when data changes so everything updates seamlessly, without you needing to check constantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signals&lt;/strong&gt; are reactive state management mechanism that provide an intuitive way to manage and track state changes while improving performance by removing the need for manual subscriptions, zonal bindings, or complex state management libraries.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Nice to know that signals are *&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Declarative State Management:&lt;/strong&gt; Signals create observable values that automatically update when dependencies change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fine-Grained Reactivity:&lt;/strong&gt; Updates are localized, meaning only components or templates depending on the signal will re-render.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; Minimal boilerplate compared to other reactive paradigms like RxJS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How do we use signals?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.Create a signal with an initial value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// import signal to use
import { signal } from "@angular/core";

// define a signal with intial value
const _counter = signal(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Read a signal value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_counter()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.To change the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_counter.set(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.To change the value with make some login&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_counter.update((value) =&amp;gt; {
   if(value == 0) return 0;

   return value - 1;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Derived signals calculate values based on other signals; this will create a new signal based on the current signal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const doubleCounter = computed(() =&amp;gt; _counter() * 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6.To create a signal from observable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const rxSignal = fromObservable(of(1, 2, 3)); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.To create an observabel from a signal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obs$ = toObservable(rxSignal);
obs$.subscribe(value =&amp;gt; console.log(value));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To define readonly signal, you can use computed signal to define a signal as readonly
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// both are readonly
_counter: Signal&amp;lt;number&amp;gt; = signal(0);
_doubleCount: Signal&amp;lt;number&amp;gt; = computed(() =&amp;gt; this._counter() * 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To define writable signal, you can use default signal or WritableSignal
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// both are writable
_counter = signal(0); // default is writable
_counter: WritableSignal&amp;lt;number&amp;gt; = signal(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's go with a counter example: &lt;br&gt;
It has two components (one for view and a second for increment/decrement) and one service to manage our signal.&lt;/p&gt;

&lt;p&gt;1.Counter serive to manage our signal.&lt;br&gt;
we have three function :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;*&lt;em&gt;getCounter: *&lt;/em&gt; To read signal value.&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;increment: *&lt;/em&gt; To increment the signal value.&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;decrement: *&lt;/em&gt; To decrement the signal value and apply a condition.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { signal } from "@angular/core";

export class CounterService {
    private _counter = signal(0);

    getCounter() {
        return this._counter();
    }

    increment() {
        this._counter.update((value) =&amp;gt; value + 1);
    }

    decrement() {
        this._counter.update((value) =&amp;gt; {
            if(value == 0) return 0;

            return value - 1;
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;2.Control component that uses the counter service and increments/decrements the signal value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class CounterControlsComponent {
  constructor(public counterService: CounterService){}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button (click)="counterService.increment()"&amp;gt;Increment&amp;lt;/button&amp;gt;
&amp;lt;button (click)="counterService.decrement()"&amp;gt;Decrement&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;View component that uses the counter service to read and display a signal value.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class CounterViewComponent {
  constructor(public counterService: CounterService){}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;Counter : {{counterService.getCounter()}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you should see the view like this, and when you click on decrement when the value is 0 , it shouldn't make anything, just display 0&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%2Ff1aev4e6th1xrf9hv336.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%2Ff1aev4e6th1xrf9hv336.png" alt="Image description" width="221" height="93"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;signals&lt;/strong&gt; for localized and component-level state.&lt;/li&gt;
&lt;li&gt;Prefer &lt;strong&gt;computed signals&lt;/strong&gt; for derived state instead of duplicating logic.&lt;/li&gt;
&lt;li&gt;Combine with RxJS when dealing with streams or external observables.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>angular</category>
      <category>programming</category>
    </item>
    <item>
      <title>First time to read about Kafka</title>
      <dc:creator>Eslam Heikal</dc:creator>
      <pubDate>Thu, 18 Aug 2022 01:46:00 +0000</pubDate>
      <link>https://dev.to/heikale/i-didnt-know-kafka-before-1c8a</link>
      <guid>https://dev.to/heikale/i-didnt-know-kafka-before-1c8a</guid>
      <description>&lt;p&gt;If you don’t know anything about Kafka this article for you , we are going to start a short journey to get to know Kafka. Now let’s go.&lt;/p&gt;

&lt;p&gt;Suppose we have two parties on applications (&lt;strong&gt;A and B&lt;/strong&gt;) and we need to exchange a message which may be is (Text, JSON), What should we do?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ooq8m4pgivtmorw7lou.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ooq8m4pgivtmorw7lou.JPG" alt="Image description" width="585" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First thing to think we will build an APIs in both, It’s good if you have a short message and will be between two apps only. &lt;/p&gt;

&lt;p&gt;Now let’s imagine an application &lt;strong&gt;A&lt;/strong&gt; is an online customer service that receive requests from the customer.&lt;br&gt;
And an application &lt;strong&gt;B&lt;/strong&gt; is for employees to display the customer requests and working on them.&lt;br&gt;
And using an APIs application &lt;strong&gt;A&lt;/strong&gt; will receive the requests from all users and send them to application &lt;strong&gt;B&lt;/strong&gt; which is store them into data storage then employees can display the request and working on them.&lt;/p&gt;

&lt;p&gt;Maybe you think, we can integrate both with one data storage application &lt;strong&gt;A&lt;/strong&gt; receive requests and store them directly then application &lt;strong&gt;B&lt;/strong&gt; will read them directly it will be good if application &lt;strong&gt;A&lt;/strong&gt; receive small number of requests.&lt;br&gt;
So what if application &lt;strong&gt;A&lt;/strong&gt; receive 100 request per minute ? &lt;br&gt;
, what if application &lt;strong&gt;A&lt;/strong&gt; receive 1000 request per minute ? &lt;br&gt;
and what if application &lt;strong&gt;A&lt;/strong&gt; receive 1000 request per second ?&lt;br&gt;
and so on …etc.&lt;/p&gt;

&lt;p&gt;Now what about the performance ? it will be no good.&lt;/p&gt;

&lt;p&gt;So integrate both applications using one database is not better solution :( .&lt;br&gt;
What if we have a solution to make application &lt;strong&gt;A&lt;/strong&gt; just receive any number of messages per second then send them to another component made to transfer a huge amount of data quickly 😊 it will be good, right ?&lt;/p&gt;

&lt;p&gt;Yes, you right This is &lt;strong&gt;Kafka&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Simply we can say Kafka is a platform which is used to handle real-time data storage and exchange message (data) between two parties.&lt;br&gt;
When you work with Kafka will listen some of words like :&lt;br&gt;
 Message, Topic, Partition, Producer, Consumer…etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6zilj04uykmzt09qkk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6zilj04uykmzt09qkk6.png" alt="Image description" width="589" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anything will be exchange between two parties called &lt;strong&gt;Message&lt;/strong&gt;.&lt;br&gt;
Now we will transfer data from application A to application B but using Kafka.&lt;br&gt;
Application A will send a message This is known as a &lt;strong&gt;Producer&lt;/strong&gt;.&lt;br&gt;
Application B will receive a message This is known as a &lt;strong&gt;Consumer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s image a scenario, application A (Producer) will receive a huge number of requests at the same time, it will to send them to Kafka directly. GOOD !.&lt;/p&gt;

&lt;p&gt;Kafka will receive them and need to store them in place till application B (Consumer) receive them, This place known as &lt;strong&gt;Topic&lt;/strong&gt;.&lt;br&gt;
Topic is a place which use to store a messages, it identified by name each topic has unique name because this name used by producer to send a message to it.&lt;/p&gt;

&lt;p&gt;Let’s say we have a topic called *&lt;em&gt;company_customer_services_topic *&lt;/em&gt; on Kafka, when you implement the producer you will add the topic name in configuration setup to know the producer, where it can send the messages?&lt;/p&gt;

&lt;p&gt;Now our messages store on the topic on Kafka, and application B need those messages (Also when you implement application B as Consumer will add topic name in configuration setup to know where it can get the message? )&lt;br&gt;
Actually, the consumer app always talking with Kafka to tell it, I’m here if and message stored in topic &lt;strong&gt;company_customer_services_topic&lt;/strong&gt; I need to get it.&lt;br&gt;
So now the consumer will pull the messages from the topic and store it in the database, and can work anything with any request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;br&gt;
Now both applications talk together via Kafka to improve the performance of exchange data, application A(&lt;strong&gt;Producer&lt;/strong&gt;) just receive a requests from customers and produce them to Kafka in specific Topic in the same time application B talk Kafka to consume/subscribe the same topic, once the message arrives and stores in the topic application B(&lt;strong&gt;Consumer&lt;/strong&gt;) will pull it .&lt;/p&gt;

</description>
      <category>programming</category>
      <category>opensource</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
