<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amir Kenarang on Medium]]></title>
        <description><![CDATA[Stories by Amir Kenarang on Medium]]></description>
        <link>https://medium.com/@amirkenarang?source=rss-3418f2f68d44------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fmGxTfN1XUaN5YJNr9Uiow.jpeg</url>
            <title>Stories by Amir Kenarang on Medium</title>
            <link>https://medium.com/@amirkenarang?source=rss-3418f2f68d44------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 10:12:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amirkenarang/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Install Kafka]]></title>
            <link>https://amirkenarang.medium.com/install-kafka-1d5e0b46542d?source=rss-3418f2f68d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d5e0b46542d</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[kafka-producer]]></category>
            <category><![CDATA[kafka-connect]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Amir Kenarang]]></dc:creator>
            <pubDate>Sun, 16 Apr 2023 10:44:53 GMT</pubDate>
            <atom:updated>2023-04-16T11:03:41.992Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Kafka</h3><p>Apache Kafka is a distributed event store and stream-processing platform. It is an open-source system developed by the Apache Software Foundation and written in Java and Scala. The project aims to provide a unified, high-throughput, low-latency platform for handling real-time data feeds.</p><figure><img alt="Install Kafka" src="https://cdn-images-1.medium.com/max/1024/1*6-CDUwitZ3sRbpCXgMkQ0w.jpeg" /></figure><h3>Install Kafka</h3><p>💡 <em>Your local environment must have Java 8+ installed.</em></p><h3>Downloading and extracting the Kafka Binaries</h3><p><a href="https://www.apache.org/dyn/closer.cgi?path=/kafka/3.4.0/kafka_2.13-3.4.0.tgz">Download</a> the latest Kafka release and extract it:</p><pre>curl &quot;&lt;https://dlcdn.apache.org/kafka/3.4.0/kafka_2.13-3.4.0.tgz&gt;&quot; -o ~/Downloads/kafka.tgz<br># Or download this version<br># curl &quot;&lt;https://archive.apache.org/dist/kafka/2.8.2/kafka_2.12-2.8.2.tgz&gt;&quot; -o ~/Downloads/kafka2.tgz</pre><pre>mkdir ~/kafka &amp;&amp; cd ~/kafka<br>#You specify the --strip 1 flag to ensure that the archive&#39;s contents are extracted in ~/kafka/ itself and not in another directory<br>tar -xvzf ~/Downloads/kafka.tgz --strip 1</pre><p><em>I’m installing Kafka in Ubuntu 22</em></p><h3>Clean Previous Configuration</h3><p>Stop Service:</p><pre>service zookeeper stop<br>service kafka stop</pre><p>Clean kafka and zookeeper data:</p><pre># Some files may not exist<br>sudo rm -rf /tmp/zookeeper<br>sudo rm -rf /tmp/kafka-logs<br>sudo rm -rf /etc/systemd/system/zookeeper.service<br>sudo rm -rf /etc/systemd/system/multi-user.target.wants/zookeeper.service<br>sudo rm -rf /etc/systemd/system/multi-user.target.wants/kafka.service<br>sudo rm -rf /etc/systemd/system/kafka.service</pre><pre>systemctl daemon-reload</pre><h3>Start Kafka without Auth</h3><p>if you want to use Kafka with Auth skip this section and follow <strong>Configuring the Kafka Server with Auth</strong> section.</p><pre>cd /home/kafka<br><br># start zookeeper<br>bin/zookeeper-server-start.sh -daemon config/zookeeper.properties<br># start kafka<br>bin/kafka-server-start.sh config/server.properties</pre><p>Follow the <strong>Manage Topic</strong> section to test Kafka.</p><h3>Configuring the Kafka Server with Auth</h3><h4><strong>Config server.properties</strong></h4><p>Kafka’s configuration options are specified in server.properties. Open this file with the nano or your favorite editor:</p><pre>nano /home/kafka/config/server.properties</pre><p>First, add a setting that will allow you to delete Kafka topics. Add the following line to the bottom of the file:</p><pre>delete.topic.enable = true</pre><p>and add this at the end for <strong>authorization</strong> to enable authentication:</p><pre>security.inter.broker.protocol=SASL_PLAINTEXT<br>sasl.mechanism.inter.broker.protocol=PLAIN<br>sasl.enabled.mechanisms=PLAIN<br><br>authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer<br>allow.everyone.if.no.acl.found=true<br>listeners=SASL_PLAINTEXT://0.0.0.0:9092<br>advertised.listeners=SASL_PLAINTEXT://:9092</pre><blockquote>💡 If you can not connect to Kafka remotely you should make this change:</blockquote><pre># This is important to connect from outside<br>advertised.listeners=SASL_PLAINTEXT://YOUR_EXTERNAL_IP_HEAR:9092</pre><p>Save and close the file.</p><h4><strong>Config zookeeper_jaas.conf</strong></h4><p>Create zookeeper_jaas.conf file and set config:</p><pre>cd /home/kafka <br>nano config/zookeeper_jaas.conf</pre><p>And add this:</p><pre>Server {<br>    org.apache.kafka.common.security.plain.PlainLoginModule required<br>    username=&quot;admin&quot;<br>    password=&quot;123456&quot;<br>    user_admin=&quot;123456&quot;;<br>};</pre><h4><strong>Config kafka_server_jaas.conf</strong></h4><p>Create kafka_server_jaas.conf file and set config:</p><pre>cd /home/kafka <br>nano config/kafka_server_jaas.conf</pre><p>And add this:</p><pre>KafkaServer {<br>  org.apache.kafka.common.security.plain.PlainLoginModule required<br>  username=&quot;admin&quot;<br>  password=&quot;123456&quot;<br>  user_admin=&quot;123456&quot;;<br>};<br><br>Client {<br>  org.apache.kafka.common.security.plain.PlainLoginModule required<br>  username=&quot;admin&quot;<br>  password=&quot;123456&quot;;<br>};</pre><h4><strong>Config zookeeper.properties</strong></h4><p>run nano config/zookeeper.properties</p><pre>authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider<br>requireClientAuthScheme=sasl<br>jaasLoginRenew=3600000</pre><h4><strong>KAFKA_OPTS in zookeeper-server-start.sh</strong></h4><p>you have to add KAFKA_OPTS to .sh or systemd service:</p><p>add the bellow code to “zookeeper-server-start.sh”</p><pre># Note: Change file address<br>export KAFKA_OPTS=&quot;-Djava.security.auth.login.config=/home/kafka/config/zookeeper_jaas.conf&quot;</pre><p>or add the bellow code to /etc/systemd/system/zookeeper.service:</p><pre>Environment=&quot;KAFKA_OPTS=-Djava.security.auth.login.config=/home/kafka/config/zookeeper_jaas.conf&quot;</pre><h4><strong>KAFKA_OPTS in kafka-server-start.sh</strong></h4><p>you have to add KAFKA_OPTS to .sh or systemd service:</p><p>add the bellow code to “kafka-server-start.sh”</p><pre># Note: Change file address <br>export KAFKA_OPTS=&quot;-Djava.security.auth.login.config=/home/kafka/config/kafka_server_jaas.conf&quot;</pre><p>or add the bellow code to /etc/systemd/system/zookeeper.service:</p><pre>Environment=&quot;KAFKA_OPTS=-Djava.security.auth.login.config=/home/kafka/config/kafka_server_jaas.conf&quot;</pre><h3>Start Kafka</h3><p>Apache Kafka could start with a bash file or with a service</p><h3>Create Kafka Service</h3><p>We want to create systemed Unit Files and Start the Kafka Server and Zookeeper Server.</p><p>These files will help you perform common service actions such as starting, stopping, and restarting Kafka in a manner consistent with other Linux services.</p><p>Kafka uses Zookeeper to manage its cluster state and configurations. You’ll use Zookeeper as a service with these unit files.</p><p>Create the unit file for the zookeeper:</p><pre>sudo nano /etc/systemd/system/zookeeper.service</pre><p>Enter the following unit definition into the file:</p><pre>[Unit]<br>Requires=network.target remote-fs.target<br>After=network.target remote-fs.target<br><br>[Service]<br>Type=simple<br>User=root<br>Environment=&quot;KAFKA_OPTS=-Djava.security.auth.login.config=/home/kafka/config/zookeeper_jaas.conf&quot;<br>ExecStart=/home/kafka/bin/zookeeper-server-start.sh /home/kafka/config/zookeeper.properties<br>ExecStop=/home/kafka/bin/zookeeper-server-stop.sh<br>Restart=on-abnormal<br><br>[Install]<br>WantedBy=multi-user.target</pre><ul><li>The [Unit] section specifies that Zookeeper requires networking and the filesystem to be ready before starting.</li><li>The [Service] section specifies that systemd should use the zookeeper-server-start.sh and zookeeper-server-stop.sh shell files for starting and stopping the service. It also specifies that Zookeeper should be restarted if it exits abnormally.</li></ul><p>After adding this content, save and close the file.</p><p>Next, create the systemd service file for kafka:</p><pre>[Unit]<br>Requires=zookeeper.service<br>After=zookeeper.service<br><br>[Service]<br>Type=simple<br>User=root<br>Environment=&quot;KAFKA_OPTS=-Djava.security.auth.login.config=/home/kafka/config/kafka_server_jaas.conf&quot;<br>ExecStart=/bin/sh -c &#39;/home/kafka/bin/kafka-server-start.sh /home/kafka/config/server.properties &gt; /home/kafka/kafka.log 2&gt;&amp;1&#39;<br>ExecStop=/home/kafka/bin/kafka-server-stop.sh<br>Restart=on-abnormal<br><br>[Install]<br>WantedBy=multi-user.target</pre><p>The following unit definition into the file:</p><p>The [Unit] section specifies that this unit file depends on zookeeper.service, which will ensure that zookeeper gets started automatically when the kafka service starts.</p><p>The [Service] section specifies that systemd should use the kafka-server-start.sh and kafka-server-stop.sh shell files for starting and stopping the service. It also specifies that Kafka should be restarted if it exits abnormally.</p><p>Now that you have defined the units, start Kafka with the following command:</p><pre>systemctl daemon-reload</pre><pre># Start Service<br>sudo systemctl start kafka.service</pre><p>To ensure that the server has started successfully, check the journal logs for the kafka unit:</p><pre>sudo systemctl status kafka.service</pre><p>You will receive output like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b6b8RhWugKxc5nrSg4LKJQ.png" /><figcaption>Kafka status</figcaption></figure><p>You now have a Kafka server listening on port 9092, which is the default port the Kafka server uses.</p><p>You have started the kafka service. But if you reboot your server, Kafka will not restart automatically. To enable the kafka service on server boot, run the following command:</p><pre>sudo systemctl start zookeeper.service</pre><p>Then run this command:</p><pre>sudo systemctl enable kafka.service<br>sudo  systemctl enable zookeeper.service</pre><p>In this step, you started and enabled the kafka and zookeeper services. In the next step, you will check the Kafka installation.</p><h3>Manage Topics</h3><h3>Create Topic</h3><p>Before you can write your first events, you must create a topic. Open another terminal session and run:</p><pre># create Topic &quot;test&quot;<br>bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test</pre><p>All of Kafka’s command line tools have additional options: run the kafka-topics.sh command without any arguments to display usage information. For example, it can also show you details such as the partition count of the new topic:</p><pre>bin/kafka-topics.sh --describe --topic test --bootstrap-server localhost:9092</pre><pre># Show Results like this:<br># Topic: quickstart-events        TopicId: NPmZHyhbR9y00wMglMH2sg PartitionCount: 1       ReplicationFactor: 1	Configs:<br>#    Topic: quickstart-events Partition: 0    Leader: 0   Replicas: 0 Isr: 0</pre><h3>Write on Topic</h3><p>A Kafka client communicates with the Kafka brokers via the network for writing (or reading) events. Once received, the brokers will store the events in a durable and fault-tolerant manner for as long as you need — even forever.</p><p>Run the console producer client to write a few events into your topic. By default, each line you enter will result in a separate event being written to the topic.</p><pre># post to the topic &quot;test&quot;<br>bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test</pre><pre># This is my first event<br># This is my second event</pre><p>You can stop the producer client with Ctrl-C at any time.</p><h3>Read the Event</h3><p>Open another terminal session and run the console consumer client to read the events you just created:</p><pre># consume from topic &quot;test&quot;<br>bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning</pre><pre># This is my first event<br># This is my second event</pre><h3>Test With Sample Code</h3><p>You can Test your kafka server with kafkajs client. There is an sample source code in the <a href="https://github.com/amirkenarang/kafka-client-sample">github</a>.</p><h3>References</h3><ul><li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-apache-kafka-on-ubuntu-20-04">How To Install Apache Kafka on Ubuntu 20.04 | DigitalOcean</a></li><li><a href="https://kafka.apache.org/documentation/#security_overview">Apache Kafka</a></li><li><a href="https://hub.docker.com/r/bitnami/kafka">Docker</a></li><li><a href="https://codeforgeek.com/how-to-set-up-authentication-in-kafka-cluster/">How to Set Up Authentication in Kafka Cluster | CodeForGeek</a></li><li><a href="https://developer.ibm.com/tutorials/kafka-authn-authz/">IBM Developer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d5e0b46542d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Publish Typescript Package in NPM]]></title>
            <link>https://amirkenarang.medium.com/publish-typescript-package-in-npm-b643c4626bd5?source=rss-3418f2f68d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/b643c4626bd5</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[gulp]]></category>
            <dc:creator><![CDATA[Amir Kenarang]]></dc:creator>
            <pubDate>Wed, 26 Jan 2022 10:02:08 GMT</pubDate>
            <atom:updated>2022-01-26T10:02:08.304Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<p>In this tutorial, we will create a tutorial to publish the package on NPM. There are some steps we should follow to publish a primitive version. In this project, we used <strong>Typescript, JavaScript, </strong>and<strong> </strong><a href="https://gulpjs.com/"><strong>Gulp</strong></a><strong>js.<br></strong>This is what we are going to build:<br><a href="https://www.npmjs.com/package/persian-normalizer"><em>https://www.npmjs.com/package/persian-normalizer</em></a></p><ul><li>Prepare Environment</li><li>Create NPM Account</li><li>Create repository</li><li>Develop, Config and, Publish</li></ul><p>If you are familiar with simple Node programming issues, skip the first 3 steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*cbQiQANqypA5jSlfY4dJ2w.png" /></figure><h3>Prepare Environment:</h3><p>Firstly Install <a href="https://nodejs.org/en/">NodeJs</a> and check <em>node</em> and <em>npm </em>versions with the following commands.</p><pre>node -v // v14.16.0<br>npm -v // 7.13.0</pre><p>I published with these versions. In the next step, I suggest installing <a href="https://code.visualstudio.com/download"><em>VSCode</em></a> in your system for a better experience in developing. In Addition, to developing installing git is required.</p><h3>Create NPM Account</h3><p>npm is the package manager for the Node JavaScript platform. It puts modules in place so that, Node can find them, and manages dependency conflicts intelligently. Most commonly, it is used to publish, discover, install, and develop node programs. Installing in npm website is incredibly simple. Just go to <a href="https://www.npmjs.com/">www.npmjs.com</a> and click on signup. Then run npm login in your command prompt and make sure that you logged in.</p><h3>Create repository</h3><p>In Github create a repository and clone on your machine. Npm package names are lowercase and words separated with <em>dash(-). </em>It’s better to create a repository with these specifications. I created this <a href="https://github.com/amirkenarang/persian-normalizer">repo</a> in Github.<br>In the empty project, run npm init , and fill-in questions. Finally, a package.json will be created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98933aa73991b22bb72d1aad3f3a2cf7/href">https://medium.com/media/98933aa73991b22bb72d1aad3f3a2cf7/href</a></iframe><h3>Develop, Config and, Publish</h3><p>In the following steps, I want to create a sample javascript package and test it locally in your machine. Create index.js and write this code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f5c5ff67d5e1f9a87c070afd1aca0f3/href">https://medium.com/media/5f5c5ff67d5e1f9a87c070afd1aca0f3/href</a></iframe><h4>Make Sure Your Package Installs and Works</h4><p>You should test your code before publishing and, you should be able to install it locally. At the root of your package, do this:</p><pre>npm install . -g<br>npm link</pre><p>Use npm ls -g to see if it&#39;s there. To test a local install, go into some other folder, and then do:</p><pre>cd ../some-other-folder<br>npm install my-package</pre><p>Use Like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0df0789016e1544c223b9934c836cf0e/href">https://medium.com/media/0df0789016e1544c223b9934c836cf0e/href</a></iframe><h4>Develop Typescript and Config It</h4><p>Now it’s time to develop your code and prepare it for publication in the public NPM repository. Change your js files to ts and add a tsconfig.json. You can generate with tsc --init and set your configs. It should be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46a16aa76f3b9bfe4f21b1fa802c4dff/href">https://medium.com/media/46a16aa76f3b9bfe4f21b1fa802c4dff/href</a></iframe><p><strong>Don’t forget</strong> to add &quot;declaration&quot;: true in tsconfig.json and, also add these configs to package.json Because when you import the library, this tells the TypeScript compiler where to look for your library’s types.</p><pre>&quot;main&quot;: &quot;dist/index.js&quot;,<br>&quot;types&quot;: &quot;dist/index.d.ts&quot;,</pre><p>Add dist folder to .gitignore and .npmignore files.</p><h4>Build and config Gulp</h4><p>Install typescript in your machine and run thetsc command at the root of the project. It will create adistfolder and build the project into it. After building you can see that there are two files with .d.ts and .js extension.<br>in my project, there are two JSON files that are static and I should copy them into the dist folder to my code work well. Here I used gulp to run the task. gulp is an open-source JavaScript toolkit. I believe that this is an awesome toolkit for js developers.<br>This is my gulp task code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d957df02257d185c9a126977119032d2/href">https://medium.com/media/d957df02257d185c9a126977119032d2/href</a></iframe><h4>Publish into NPM</h4><p>Make sure your Readme.md , CHANGELOG.md , CONTRIBUTING.md and LICENCE files are ready to publish. that&#39;s not necessary but it’s better to have this information in your project. Now I created the final package.json and publish it with this command</p><pre>npm run publish</pre><p>The prepublish the script, run gulp tasks and build typescript. The final package.json is like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6452e0cad6ba9edd205e5d056e636ae/href">https://medium.com/media/c6452e0cad6ba9edd205e5d056e636ae/href</a></iframe><p>Boooom! We got a package.</p><p>I hope you had liked this tutorial. Good luck with publishing the package in NPM</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b643c4626bd5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>