<?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 Ákos Rádler on Medium]]></title>
        <description><![CDATA[Stories by Ákos Rádler on Medium]]></description>
        <link>https://medium.com/@kosrdler?source=rss-71008470ec49------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OILd07rOStgZcEZuwYO8SA.jpeg</url>
            <title>Stories by Ákos Rádler on Medium</title>
            <link>https://medium.com/@kosrdler?source=rss-71008470ec49------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:48:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kosrdler/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[Configuring Storybook for Next.Js, I18n & TypeScript]]></title>
            <link>https://medium.com/@kosrdler/configuring-storybook-for-next-js-i18n-typescript-a9119f13e023?source=rss-71008470ec49------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9119f13e023</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[storybook]]></category>
            <dc:creator><![CDATA[Ákos Rádler]]></dc:creator>
            <pubDate>Sat, 21 Dec 2019 20:22:37 GMT</pubDate>
            <atom:updated>2019-12-21T20:22:37.185Z</atom:updated>
            <content:encoded><![CDATA[<h3>Configuring Storybook for Next.Js, I18n, SCSS &amp; TypeScript</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tztDksluOQcMI9G9RGui0A.png" /></figure><p>Storybook is great. It lets you design, tweak and test your components in complete isolation. Integrating it is usually not that big of a deal, but there are some hoops when it comes to integrating into an existing framework, such as NextJs.</p><h3>Quick start</h3><p>In order to add Storybook to your project, as the official quick start guide suggests, simply run:</p><pre>npx -p @storybook/cli sb init</pre><p>This will take care of the initial setup. It’ll:</p><ul><li>add the packages</li><li>add the package scripts</li><li>add the config files under .storybook</li></ul><h3>Environment variables</h3><p>Chances are, you’re storing sensitive information (and probably not so sensitive as well) in a .env file. Next knows about it, via next.config.js, and takes care of exposing this information to the client code. However, since next isn’t running side-by-side with Storybook, this task falls onto us.</p><p>Fortunately, we don’t have to do much. We can make the publicRuntimeConfig available by navigating to your storybook configuration in .storybook/config.js and adding this piece of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d041d4c3e15171c1aed549e605f84fe9/href">https://medium.com/media/d041d4c3e15171c1aed549e605f84fe9/href</a></iframe><h3>Header links</h3><p>Using custom fonts? CDN linked stylesheets? Not a problem.</p><p>All you need to do is to create a <strong><em>preview-head.html </em></strong>file inside your .storybook folder and add all your <em>head </em>contents there.</p><h3>TypeScript</h3><p>Your stories are going to be built with Webpack, so all you need here is a standard webpack configuration for TypeScript.</p><p>First, create webpack.config.js file in your .storybook folder. Then, add the webpack config, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eafa135034ef1c62ded71c177ae09317/href">https://medium.com/media/eafa135034ef1c62ded71c177ae09317/href</a></iframe><h3>SCSS</h3><p>SCSS configuration could not be easier with this handy package. Setup is very straightforward, so I’ll just link it:</p><p><a href="https://www.npmjs.com/package/@storybook/preset-scss">@storybook/preset-scss</a></p><h3>I18N</h3><p>Translations! Gotta love ’em. There are a number of flavors of i18n, but since we’re talking Next, I’ll assume you’re using <a href="https://github.com/isaachinman/next-i18next">Next-I18Next</a>. You obliously won’t need the SSR capabilities the package provides, but you probably don’t want to install another translation package just for Storybook. That’s alright, we can make it work.</p><p>First, if you’re using module.exports in your i18n configuration, change it to ES6 style. This is necessary since you’ll need to export individual parts of your NextI18Next instance, and the two export styles should not be mixed.</p><p>Once you have:</p><pre><strong>export default NextI18NextInstance;</strong></pre><p>Also export:</p><pre><strong>export const {appWithTranslation, Link, Trans, Router, config, withTranslation, i18n} = NextI18NextInstance;</strong></pre><p>Currently next-i18next depends on a middleware to load namespaces. Since that runs on server side, we don’t have access to said middleware. So, what do we do?</p><p>Well, what we can do, is that we can invoke the translation service client side.</p><p>First, we have to expose the static directory containing your translations publicly (as Next does). That’s easily done, just modify your storybook script in package.json to:</p><pre>&quot;storybook&quot;: &quot;start-storybook -s ./static/ -p 6006&quot;</pre><p>Then, in order to invoke the translation service, you’ll need to wrap your component with <strong><em>appWithTranslation </em></strong>once you write your stories, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/550564f6a469995a3318544cf2b4495e/href">https://medium.com/media/550564f6a469995a3318544cf2b4495e/href</a></iframe><h3>Material UI</h3><p>If you have a project that heavily uses Material-UI, you’ll want your stories to have access to it’s predefined themes. This can be done on the story level, or globally as well.</p><p>You could leverage <a href="https://github.com/react-theming/storybook-addon-material-ui">storybook-material-ui</a> however, at the time of writing, it only handles palette definitions and custom typography, but not component level overrides.</p><p>If you need that, like I did, you’ll need to add the provider decorator manually.</p><p>Create a MuiDecorator file somewhere in your project:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0b4b28b9caba196ec0819326d6ab78a/href">https://medium.com/media/b0b4b28b9caba196ec0819326d6ab78a/href</a></iframe><p>Then, in your storybook config file, import addDecorator from @storybook/react, and call:</p><pre><strong>addDecorator(MuiDecorator);</strong></pre><p>before your <em>configure.</em></p><p>Hope this quick guide helps you getting Storybook up and running!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9119f13e023" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[useMemo with dependencies — a simple case study]]></title>
            <link>https://medium.com/@kosrdler/usememo-with-dependencies-a-simple-case-study-11ec58561553?source=rss-71008470ec49------2</link>
            <guid isPermaLink="false">https://medium.com/p/11ec58561553</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ákos Rádler]]></dc:creator>
            <pubDate>Tue, 09 Jul 2019 12:33:32 GMT</pubDate>
            <atom:updated>2019-07-09T12:33:32.213Z</atom:updated>
            <content:encoded><![CDATA[<h3>useMemo with dependencies — a simple case study</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AqKB-9-R60OaxhTCX1WYVg.jpeg" /></figure><p>When React 16.8 came out, it completely changed how we write our components. State, lifecycle, refs are now managed with hooks, and in turn the code you write became more comprehensive, and less prone to issues.</p><p>Let me start off by stating that this post is not meant to be a thorough explanation of hooks, or <em>useMemo</em>, though I hope it gives you a better understanding them.</p><h3>UseMemo</h3><p><strong>useMemo </strong>is a React hook, that takes a function, and returns a memoized value. Essentially, it’s caching the values the function returns after its initial execution, which makes it your go-to tool for performance optimization.</p><p><em>But wait, don’t we already have React.Memo for this?</em></p><p>Kind of…</p><p>Unlike <em>React.Memo</em>, which is a higher order component and meant to wrap other components, <em>useMemo </em>memoizes function values.</p><p>…and since class components and stateless functional components merged into <strong>Functional Components,</strong> that are essentially functions themselves, <em>useMemo </em>is a perfect fit for caching our components going forward.</p><p><em>useMemo </em>is also more general, in the sense that it takes an array of dependencies, meaning that the memoized value will only get recomputed if one of the specified dependencies change.</p><p>In order to understand its workings better, let’s take a look at an example of an issue I was facing before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*YeTAYG-RYsRMbJ8o-N-Ehg.png" /></figure><p>We have a list of products, all ready to go into a shopping cart, wrapped in <em>useMemo:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd558f8e03f4ac8279ca7df01e45bf6c/href">https://medium.com/media/fd558f8e03f4ac8279ca7df01e45bf6c/href</a></iframe><p>When we do add to the cart though, the <strong>ProductList </strong>component re-renders. That is not what we expected to see, our product list did not change at all.</p><p>Looking at it with the handy tool why-did-you-re-render, we immediately see the culprit:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*YPIYJTlcFbe_8_8D_HQplA.png" /></figure><p>Since the shopping cart updates the state on the parent component, it causes it to re-render, which recreates the <em>handleAddToCart </em>function. Since <em>handleAddToCart</em> is a dependency on our <em>useMemo </em>in <strong>ProductList</strong>, it discards the memoized component value.</p><h3>How do we fix this?</h3><p>There are a number of approaches we can take:</p><ul><li><em>Refactor!</em></li></ul><p>If this was your first thought, congratulations for having the right mindset :)</p><p>Although keep in mind that this is just an example project. In the real world, there are times when you need a quick solution.</p><ul><li><em>Can’t we just remove handleAddToCart from the dependencies?</em></li></ul><p>That won’t solve our issue. Remember, the dependency list is there to specify what gets shallow compared for memoization, not what gets passed down.</p><p>Let’s see what happens if we do go down that path. We’ll remove the dependency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*4CTEQcIOFjfxaMqFwPsOsw.gif" /></figure><p>That ain’t right. What’s happening?</p><p>Since we removed <em>handleAddToCart </em>from the dependencies, when our parent component re-rendered, the function was recreated. However the <strong>ProductList </strong>component still held the memoized function.</p><p>What we ended up with essentially, is two different functions with the same name on our component. React even warns us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*NQcs4T9TBlGLGaMJ_Lw9BQ.png" /></figure><p>It’s as if warnings should be taken seriously ;)</p><h3>Finding a solution</h3><p>There are multiple ways we can resolve this. One quick and easy solution is to leverage another hook, <em>useCallback</em>.</p><p><em>useCallback </em>works much in the same way as <em>useMemo</em>. The difference is that while <em>useMemo </em>returns a memoized value, <em>useCallback </em>returns a memoized function, which is just what we need.</p><p>On our parent component, let’s wrap our <em>handleAddToCart </em>in a <em>useCallback</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c02da9d45fa63f88429bcded3dd26f32/href">https://medium.com/media/c02da9d45fa63f88429bcded3dd26f32/href</a></iframe><p>No re-renders this time!</p><h3>The useReducer approach</h3><p>There is another neat trick you can pull, especially if you either have complex state in your parent component, or you’re passing down multiple functions to manipulate said state.</p><p>If you define your state using <em>useReducer</em>, you could pass down its dispatch function to your child components, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/644/1*y5NxR8TNje9HOAp7Am9z9w.png" /></figure><p>The <em>dispatch</em><strong> </strong>callback won’t get recreated on state changes, which saves us from the need to wrap multiple functions in <em>useCallback</em>s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*qcglxthR8DhKx9MygEJQBg.png" /></figure><p>Invoking our dispatch…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*ZL0AxpJPrQYdHal9WoN_cw.png" /></figure><p>…and green all around.</p><p>That’s it, I hope you had as much fun reading as I did writing it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11ec58561553" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interceptors and Aspect Oriented Programming in Microsoft Orleans]]></title>
            <link>https://medium.com/@kosrdler/interceptors-and-aspect-oriented-programming-in-microsoft-orleans-a1d12b7bb1fc?source=rss-71008470ec49------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1d12b7bb1fc</guid>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[microsoft-orleans]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[aop]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Ákos Rádler]]></dc:creator>
            <pubDate>Fri, 23 Feb 2018 07:19:35 GMT</pubDate>
            <atom:updated>2018-02-23T07:19:35.929Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3xT7fkqFuVp2uIiknrNsUA.png" /></figure><p>In today’s world, where services need to handle thousands of requests per second, a serious need for highly scalable systems arose, and as a solution, the actor model to meet it.</p><p>The idea of the actor model came from the 70’s, and since then, the world has seen many implementations and frameworks of it. The one I’m going to be using is <a href="http://dotnet.github.io/orleans/index.html">Microsoft Orleans</a>.</p><p>In the actor model, the actors are the building blocks of your system. In Orleans, they’re referred to as <strong><em>Grains</em></strong>.</p><p>They’re described as:</p><blockquote>atomic units of isolation, distribution, and persistence</blockquote><p>In Layman’s terms, your system is broken down into small executions, interacting with each other. There can be thousands of them. <strong>Millions</strong>.</p><p>Working with an actor framework requires a different outlook on how we build software. The logic in your code is going to be more fragmented, leading to the issue, that the number of places you’d potentially need to add metrics/logging or exception handling increases. Luckily, Orleans provides an easy way to implement the aspect oriented way of providing logging.</p><p><em>Aspect oriented programming</em> addresses the problem of common patterns of logic being scattered across your codebase, providing a way of refactoring them and encapsulating them into a single well defined space.</p><p>Just based on the sheer amount of grain types one could possibly have in the system, the codebase could very soon show patterns of the following popping up:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07c68ace826dfbd6da351a83d2c8bb9c/href">https://medium.com/media/07c68ace826dfbd6da351a83d2c8bb9c/href</a></iframe><p>In Orleans, exceptions propagate through grain calls. Wouldn’t it be nice to catch them mid-call, and perform the logging in a unified place?</p><p>We can most certainly do that! Orleans has a variety of handy features to assist us with memory storage, telemetry or bootstrapping. What we’re going to need now though is an <strong>interceptor</strong>.</p><p>Interceptors, or otherwise known as grain call filters are called before- or after a call to a grain has been made, which makes it a perfect tool to execute common logic, for example: performing cleanup or logging.</p><h3>Implementing the interceptor</h3><p>In order to have a grain call filter in your project, all you really need to do, is to have a class implement the <em>IGrainCallFilter </em>interface and register it in the <em>SiloHostBuilder</em>.</p><p>IGrainCallFilter has a single method defined:</p><pre><strong>Task Invoke(IGrainCallContext context)</strong></pre><p>The<em> IGrainCallContext </em>looks like this:</p><pre>- <strong>IAddressable </strong>Grain<em> </em>: <em>The grain being invoked</em><br>- <strong>MethodInfo </strong>Method  : <em>The method being invoked</em><br>- <strong>object[]</strong> Arguments : <em>Arguments of the invocation</em><br>- <strong>object</strong> Result      : <em>Get/Sets the results</em><br>- <strong>Task</strong> Invoke()      : <em>Invokes the request</em></pre><p>First and foremost, let’s pass in the logger of our choice, via dependency injection. Dependency injection in Orleans is using the abstraction written by the developers of <a href="https://docs.asp.net/">ASP.NET Core</a>, so if you’re coming from that environment it’s going to be very familiar to you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a2ba990e193c103cb9a777a8146ae5e/href">https://medium.com/media/5a2ba990e193c103cb9a777a8146ae5e/href</a></iframe><p>Let’s implement the Invoke method. Really, all we’re doing is wrapping the invocation in a try-catch, and logging it if an exception happens. Straightforward stuff.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40e22870644106c098a33b5048e112a9/href">https://medium.com/media/40e22870644106c098a33b5048e112a9/href</a></iframe><p>All is left is to register the call filter in the <em>SiloHostBuilder</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6329f6754a9a6faef67247a15a5f13b0/href">https://medium.com/media/6329f6754a9a6faef67247a15a5f13b0/href</a></iframe><p>And we’re done! No more try-catches sprinkled in the code for the sole purpose of logging, all of that is now in one place, which makes for cleaner, easier to read code.</p><h3>Grain calls from call filters</h3><p>Let’s explore call filters a bit further. While in a call filter, you’re free to inject your preferred logger and, well, almost anything else:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*yl_psCcJ3fqN3ZaSJuxdrQ.png" /><figcaption>Taken from dotnet/orleans gitter</figcaption></figure><p>We can certainly make use of this. What if we’d like obtain information from a grain that needs to be logged? Let’s take our logger a step further, and make it report the number of grain activations the silo had for the grain type from which the exception was caught.</p><p>The <strong>GrainFactory </strong>can be used to invoke other grains. First of all, let’s make sure the project in which the call filter resides references the grain classes.</p><p>Then, we’ll inject the <em>IGrainFactory </em>into the call filter, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39e08ef6062e82869d1b0bbbc32171b7/href">https://medium.com/media/39e08ef6062e82869d1b0bbbc32171b7/href</a></iframe><p>… then we’ll extend the invoke with the grain activation logging.</p><p>Getting this information might sound tricky. Fortunately Orleans keeps a track of a number of details of the silo in the <strong>ManagementGrain,</strong> including<strong> </strong>the number of activations of a given grain type.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2e4c5ec785a57c2ad3d27bb161b554b/href">https://medium.com/media/d2e4c5ec785a57c2ad3d27bb161b554b/href</a></iframe><p>Nice and simple, isn’t it?</p><p>I should mention that this code is mainly for demonstration purposes, and possibly not what you’d use in production.</p><p>I hope this relatively easy example on call filters gave you an idea on how and why they are an important part of Orleans.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1d12b7bb1fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wiring up ReactJS.Net with Asp.Net Core MVC via SignalR Core]]></title>
            <link>https://medium.com/@kosrdler/wiring-up-reactjs-net-with-asp-net-core-mvc-via-signalr-core-24915f8caa17?source=rss-71008470ec49------2</link>
            <guid isPermaLink="false">https://medium.com/p/24915f8caa17</guid>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[signalr]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ákos Rádler]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 14:39:49 GMT</pubDate>
            <atom:updated>2018-01-11T14:39:49.785Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kTSHHlg02IEo0G8-tlE_BA.jpeg" /></figure><p>Recently, I’ve worked on a project that involved React code embedded in Asp.Net Core, that needed to talk to third party technologies via SignalR.</p><p>Due to the fact that at the time of writing SignalR Core is in alpha2, there’s little documentation available on how integrating with it can be done, so I decided to share what I’ve learned. And what better way to do that than building something?</p><h3>What we’re building</h3><p>To keep it as brief as possible, we’re going to build a very simple web app to demonstrate the communication between SignalR and its clients.</p><p>The idea is this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*s6RS7dhHcjQEmm6L6YuGsQ.png" /><figcaption>Application Flow</figcaption></figure><p>The React component sends a message to the JavaScript controller, which in turn sends it to the Hub. The Hub then distributes it among the connected clients. We’ll also look at how it’d work the other way around. Sending a message from the Web app, which is eventually going to be received by the React component.</p><h3>Setting up the project</h3><p>You are going to need 3 things:</p><ul><li>An Asp.Net Core Web Application</li><li>The ReactJS.Net NuGet package: React.AspNet</li><li>The SignalR Core NuGet package</li></ul><p>You’ll need the Microsoft.AspNetCore.SignalR package. Keep in mind that it is in alpha, therefore, it can be found under the pre-releases.</p><h3>Adding the Hub</h3><p>The Hub is the central part of SignalR that allows for communication with multiple clients, usually via WebSockets (if available).</p><p>We’re going to make it implement a single method, for invoking <em>displayMessage() </em>on the connected clients:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b39460ab9f36bbf13c59d701a366d389/href">https://medium.com/media/b39460ab9f36bbf13c59d701a366d389/href</a></iframe><h3>Configuring the Web App</h3><p>Let’s tell the web app that we’re using SignalR and React:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/487047bd99b107261d1cf6a6d69982af/href">https://medium.com/media/487047bd99b107261d1cf6a6d69982af/href</a></iframe><p>… and let’s configure them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75cd3882e644797b57536e07d1c919d8/href">https://medium.com/media/75cd3882e644797b57536e07d1c919d8/href</a></iframe><p>You could add all your JSX files here as well with:</p><pre>.AddScript(“~/js/webApp.jsx”)</pre><p>but since we’re not planning on having any server-side rendering, we’ll leave it out.</p><p><strong>Important note</strong>: The <em>app.UseReact()</em> method call <strong>MUST</strong> come before the <em>UseStaticFiles() </em>call!</p><h3>Setting up SignalR</h3><p>First, we’ll need the SignalR JavaScript client.</p><p>Run npm i @aspnet/signalr-client or get it directly from <a href="https://blogs.msdn.microsoft.com/webdev/2017/10/09/announcing-signalr-for-asp-net-core-alpha-2/">MSDN</a>.</p><p>If you’re going the npm route, you’ll need to configure Webpack (or the tool of your choice) to deploy the packages from node_modules to the wwwroot.</p><p><strong>Note: </strong>I had some issues with the standard client, specifically when receiving invocations. If you’re experiencing the same, switch to the ES5 client.</p><p>Once you have it, you can start directly referencing the client file in either your <em>_Layout.cshtml </em>or the view you’re planning on using it in.</p><h3>Adding the controller script</h3><p>The controller is going to serve as a bridge between the hub and the client. Its responsibilities are:</p><ul><li>It starts and maintains the SignalR connection</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfe424262625a6980afffa83ef1d9d4a/href">https://medium.com/media/dfe424262625a6980afffa83ef1d9d4a/href</a></iframe><ul><li>It sends the messages by invoking the <em>SendMessage </em>method on the Hub</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57a6acb8b89d8d9a3947a8712ab83a95/href">https://medium.com/media/57a6acb8b89d8d9a3947a8712ab83a95/href</a></iframe><ul><li>It listens for messages coming from the Hub, and forwards them</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc7a8e0f7b03d086f19254e3b73729ba/href">https://medium.com/media/dc7a8e0f7b03d086f19254e3b73729ba/href</a></iframe><p>Now that we pretty much have SignalR in place, let’s move on to ReactJs.Net.</p><h3>Setting Up ReactJs.Net</h3><p>In our <em>_ViewImports.cs</em>, let’s add:</p><pre>@using React.AspNet;</pre><p>This is going to tell MVC that our .jsx script includes are JSX files that need to be compiled into JavaScript by ReactJs.Net’s internal transpiler.</p><p>Normally you’d need an external transpiler service to precompile your JSX to standard JavaScript, however, ReactJs.Net internally takes care of that for you.</p><p>Still, if you’d like, you’re welcome to disable the internal transpiler, and configure (eg.: Babel) to take care of the work.</p><p>Let’s add the <em>webApp.jsx</em> file to the project, and include it in the referenced scripts in the view.</p><p>I wrote a simple React component that:</p><ul><li>starts a connection to SignalR</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c187305034fc2794fb2d7673704dd530/href">https://medium.com/media/c187305034fc2794fb2d7673704dd530/href</a></iframe><ul><li>listens to a custom message event, and displays received messages</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/607348de5de0c413b6c8b37b2bcbec84/href">https://medium.com/media/607348de5de0c413b6c8b37b2bcbec84/href</a></iframe><ul><li>displays a standard Form control to send messages</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5fd2a9600d69360567872b73a69c002/href">https://medium.com/media/f5fd2a9600d69360567872b73a69c002/href</a></iframe><p>The component is relatively simple, it consists of a single input field and a submit button.</p><p>Let’s add the following line to the end of <em>Index.cshtml</em> after we’re done with the JavaScript and JSX imports:</p><pre><a href="http://twitter.com/Html">@Html</a>.ReactInitJavaScript();</pre><p>This will force React to render the code to initialise the component on the client.</p><p>Finally, let’s hook our React component up onto our View:</p><pre>ReactDOM.render(&lt;WebApp /&gt;, document.getElementById(&quot;webApp&quot;));</pre><h3>Let’s run it!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*8KUng2serCdSFWearV-tCA.png" /></figure><p>Ouch!</p><p>What’s happening here?</p><p>Whenever a service is requested, the dependency injection framework is performing call-site validation. The way <em>UseReact() </em>works, is that it calls <em>GetService()</em> from the origin of the call, to ensure that the requested services are available, which make the two clash.</p><p>To get around this issue, we’ll disable the validation in BuildWebHost:</p><pre>.UseDefaultServiceProvider(options =&gt; options.ValidateScopes = false)</pre><p>Now when we send the message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*f5tJ00lV-hrolydwFmEh6Q.png" /></figure><p>… we get the result!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/147/1*rs8QXn7qzQOIdv0ssrlG7g.png" /></figure><h3>Sending messages from the Web App</h3><p>Alright, what about the other way around? What if we need to send messages from our web application to other connected clients where the react components would be displaying the messages?</p><p>We can most certainly do that!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*HRyv5dWYcg0qUifnvQ-xjg.png" /><figcaption>Application flow</figcaption></figure><p>In this case, we’ll be invoking the Hub method from the MVC controller.</p><p>In order to do so, we’ll need the Hub injected into our selected controller. SignalR is already configured in <em>ConfigureServices, </em>let’s add the Hub there as well:</p><pre>services.AddSingleton&lt;MyHub&gt;();</pre><p>This will put up our Hub for DI, whenever we ask for the singleton instance for it to be injected.</p><p>In our controller, let’s inject and save a reference to our Hub:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e83bca54365d13e0cac6317647a2adef/href">https://medium.com/media/e83bca54365d13e0cac6317647a2adef/href</a></iframe><p>Also, let’s create a simple controller method for sending messages to the other clients:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ab988920fb8319b08adb8ac7b9a049a/href">https://medium.com/media/3ab988920fb8319b08adb8ac7b9a049a/href</a></iframe><p>And, that’s it!</p><p>Hopefully this mini guide helps you with SignalR’s alpha client integration. I did not, or rather, couldn’t cover the whole process, but I tried to outline the most important parts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24915f8caa17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>