<?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 Hajdarevic Haris on Medium]]></title>
        <description><![CDATA[Stories by Hajdarevic Haris on Medium]]></description>
        <link>https://medium.com/@hajdarevich?source=rss-c2d88c4b12bf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*EekgEAt6PwuIHdzVGFrrXQ.jpeg</url>
            <title>Stories by Hajdarevic Haris on Medium</title>
            <link>https://medium.com/@hajdarevich?source=rss-c2d88c4b12bf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 13:01:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hajdarevich/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[Angular — NodeJS — SocketIO — RealTime application]]></title>
            <link>https://medium.com/@hajdarevich/angular-nodejs-socketio-realtime-application-b1215d142881?source=rss-c2d88c4b12bf------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1215d142881</guid>
            <category><![CDATA[angular-4]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[socketio]]></category>
            <dc:creator><![CDATA[Hajdarevic Haris]]></dc:creator>
            <pubDate>Fri, 15 Feb 2019 19:14:58 GMT</pubDate>
            <atom:updated>2019-09-12T17:50:27.514Z</atom:updated>
            <content:encoded><![CDATA[<h3>Make realtime application with angular and node js powered by web sockets</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MyZ65DVoeSfNZ2Jqz1BPqQ.jpeg" /><figcaption>Photo source: Wikipedia</figcaption></figure><p>Probably a lot of times during the development of different apps or resolving to various tasks you have faced yourself with a massive operation in your application.</p><p>The most important thing in many cases during that kind of operation is to enable concurrency, UI non-blocking and at least to provide information about the status of background work, actually to show as a progress bar.</p><p>People can understand if something takes a lot of time in a case of heavy operation, but they will want to see the progress of that and also they will want to use the rest of the application.</p><p>That is the main topic here. We are going to simulate heavy long-running operation in the background on a server-side.</p><p>In this example we are going to use:</p><ul><li>Angular 5+</li><li>NodeJS ≥ 8.0.0</li><li>Socket.io</li><li>Socket.io client</li><li>Semantic UI</li></ul><p>I will walk you through whole application considering that you already have a solid knowledge of NodeJS &amp; Angular. Focus here is on asynchronous non-blocking heavy long-running operation &amp; informing user about things that are happening in a background…</p><p>So let’s get started</p><h3>Server-side ( nodejs + socket.io )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*BF_ifuqFRst78wCo5-rfzA.png" /><figcaption>Photo source <a href="https://medium.com/@noufel.gouirhate/build-a-simple-chat-app-with-node-js-and-socket-io-ea716c093088">https://medium.com/@noufel.gouirhate/build-a-simple-chat-app-with-node-js-and-socket-io-ea716c093088</a></figcaption></figure><p>You will see a mix of Angular dependencies and NodeJS dependencies. So the whole project contains only one package.json.</p><pre><strong>&quot;socket.io&quot;</strong>: <strong>&quot;1.7.4&quot;</strong>,</pre><p>Above is socket.io npm package for the NodeJS.</p><p>I used to express to create REST API endpoint. So if you go to look for the file in a project: “<em>/server/routes/operation/index.ts”</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26d9f35394c49d0da6d04537404a0053/href">https://medium.com/media/26d9f35394c49d0da6d04537404a0053/href</a></iframe><p>That is the main entry point for the client application. We have only one REST API endpoint “/start” which accept progressId as a request body payload.</p><p>We are going to cover three different scenarios of simulating massive operation.</p><p><strong>Massive operation 1</strong></p><ul><li>User can start operation.</li><li>No manual cancel.</li><li>No handling errors on the server for an async operation.</li><li>Not possible to <strong>restart</strong> during the execution.</li><li>This operation <strong>will finish with no errors</strong>.</li><li>User can track progress.</li></ul><p><strong>Massive operation 2</strong></p><ul><li>User can start operation.</li><li>No manual cancel.</li><li>Handling errors on the server for an async operation.</li><li>The operation will <strong>stop</strong> on 50% and <strong>new response will be returned</strong>.</li><li>User can track progress.</li></ul><p><strong>Massive operation 3</strong></p><ul><li>User can start operation.</li><li>It is possible to <strong>cancel</strong> this operation manually.</li><li>After cancel can <strong>start</strong> operation <strong>again</strong>.</li><li>User can track progress.</li></ul><p>As it is shown above in code sample we have one entry point to our server-side via “<strong>/start/” </strong>endpoint that receives <strong>progressId</strong> trough request body <strong>payload.</strong></p><p>I’m going to cover third function <em>longOperationStartAndStopOnUserCancel() and the rest two are similar.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5da2a4a9359ca6ef45b871fa46941699/href">https://medium.com/media/5da2a4a9359ca6ef45b871fa46941699/href</a></iframe><p>We have simple for loop that simulates long-running operation with a timeout of 500ms that is called in each iteration of the loop. On each iteration we are emitting progress with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c4d53a6074d4bec21d4ed48ec89fffe/href">https://medium.com/media/6c4d53a6074d4bec21d4ed48ec89fffe/href</a></iframe><h3>Client-side ( Angular + socket client )</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*Y547A1WkJDBXb4OYF_JSjg.png" /></figure><p>Now, let&#39;s move to client application.</p><p>First of all, we should make the connection to socket service and that is done in <strong>/src/app/socket/socket.service.ts</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23ed33b9c01d19266ededbd97efcc023/href">https://medium.com/media/23ed33b9c01d19266ededbd97efcc023/href</a></iframe><p>In a constructor() we are making a connection and also there is public method that checks are we connected.</p><p>Method isConnected() is used in a header of application to show are we connected or not.</p><p>&lt;<strong>header-component [isConnected]=&quot;socketService.isConnected() | </strong><em>async</em><strong>&quot;</strong>&gt;&lt;/<strong>header-component</strong>&gt;</p><p>The result is shown here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TtYH2H_5RkzEdtvm69rW5w.png" /><figcaption>Status of socket connection between client &amp; server</figcaption></figure><p>Once we are connected we should call /start endpoint on a server. That is done in <strong>app.component.ts with a method:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/face491206738e46b9036e1f946fd74a/href">https://medium.com/media/face491206738e46b9036e1f946fd74a/href</a></iframe><p>Its simple API call. So if you remember when we trigger the above API we are expecting that we get information via of progress via WebSockets. So we should set up socket listener on a channel <strong>#progress</strong> because if you remember server is emitting to that channel.</p><p>That is done with this block of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30e65f84a613dac3494163308cf9cd13/href">https://medium.com/media/30e65f84a613dac3494163308cf9cd13/href</a></iframe><p>Where response and progress are following objects</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eef4a384d5eae18ac6663a37447de7d3/href">https://medium.com/media/eef4a384d5eae18ac6663a37447de7d3/href</a></iframe><p><strong>Progress</strong> is updated for <strong>each</strong> emits and <strong>response</strong> is updated only when <strong>event.status</strong> is false that represents a signal how the operation is <strong>finished</strong>.</p><p>I’m not going to cover the template section. It is pretty straightforward how this is presented in <strong>app.component.html. </strong>I just want to mention that for progress I used <a href="https://edcarroll.github.io/ng2-semantic-ui/#/modules/progress">ng2 semantic progress module</a>.</p><h3>Cancel operation</h3><p>Cancel operation is interesting because that is communication in the opposite direction from the client to server through web sockets.</p><p>On the client side in app.component.ts there is method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d55c6b4017a1dcfb1e3a1d4ed016508a/href">https://medium.com/media/d55c6b4017a1dcfb1e3a1d4ed016508a/href</a></iframe><p>A very simple one that emit the boolean value of <strong>true</strong> to channel “cancel”.</p><p>On a <strong>server-side </strong>in <strong>app.ts </strong>I create socket listener that listens for messages from a client application on a channel cancel</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/398b20c8ef11cb928799712e2956ac90/href">https://medium.com/media/398b20c8ef11cb928799712e2956ac90/href</a></iframe><p>If the message is sent and pass condition method cancelTransaction() will be called that will update public static property <strong>canceled </strong>on class <strong>Operation </strong>to true.</p><p>If we recall <em>longOperationStartAndStopOnUserCancel() you will notice that there for each iteration we check is operation canceled and act accordingly.</em></p><h3>Repository &amp; Demo</h3><p>The demo app is deployed to Heroku and it is available on<br><a href="https://angular-nodejs-realtime-app.herokuapp.com/">https://angular-nodejs-realtime-app.herokuapp.com/</a></p><p>To get full source code go to <a href="https://github.com/harishajdarevic/angular-nodejs-socketio-semantic">GitHub link</a>.</p><h3><strong>About </strong><a href="http://innovic.io/"><strong>Innovic.io</strong></a></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*rlWlW_50m-7ySpbl1op8mA.png" /><figcaption>Innovic logo</figcaption></figure><p>Innovic doo is a software consulting company based in Sarajevo, Bosnia and Herzegovina. As a company, we are looking for long term partners interested to simplify and quicken their business.</p><p>We are proficient in building full-stack solutions, from the ground to up. We can support you all the way in your product development. Our strength is 10+ years of experience in IT business and programming.</p><p>To get in touch <br><strong>Website: </strong><a href="http://innovic.io/">http://innovic.io</a> <br><strong>Instagram</strong>: <a href="https://www.instagram.com/innovic.io/">https://www.instagram.com/innovic.io/</a><br><strong>Facebook</strong>: <a href="https://www.facebook.com/innovic.io/">https://www.facebook.com/innovic.io/</a><br><strong>Linkedin:</strong> <a href="https://www.facebook.com/innovic.io/">https://www.facebook.com/innovic.io/</a><br><strong>GitHub:</strong> <a href="https://github.com/Innovic-io">https://github.com/Innovic-io</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1215d142881" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>