Sunday, May 10, 2020

Tip: How to use HTML5 Server Side Events

Server Side Events (SSE) allows the web server (node.js, IIS, Apache, etc) to keep a connection active with the client browser while server is pushing data to the client whenever needed. However SSE is not the same as WebSockets as it can only push data from server whereas WebSockets can both receive and push data. Below is a very simple SSE application developed using node.js and HTML.


Server Code



Client Code

You can include the following snippet in the html header section
You will receive the following in Chrome debugger if the message is sent successfully from the server.

Tuesday, February 18, 2020

Apache Kafka as an integration platform and technology

I came across Apache Kafka when I was searching the web for middleware integration platform. I discovered that many people are giving good comments about Apache Kafka as a high performant publish subscribe messaging platform. Below is a sample scenario/diagram that I could think of if i am to use it to integrate different systems in an organization.

One can imagine Kafka as a highway that connects all the systems in the application, whereby in traditional design, we use many APIs to connect to different systems. Kafka also serves as a component that promote the standardization of software integration by using similar connector and message protocol.

Use Cases Of Kafka
1. Fraud detection - one can monitor the events generated in Kafka and create different use cases to detect anomaly, possible frauds before a transaction is completed.
2. Microservices - Kafka can be the backbone of the microservice architecture and even for legacy applications when the right connector is used. Event driven design and architecture has become a norm for enterprise application development and that will rely heavily on a event streaming platform.
3. Capture Web Site User Behavior - Every mouse clicks that a user does can be tracked and is useful for behavioral analysis.

Friday, January 17, 2020

gRPC, a WCF Sibling?

I came across this term "gRPC " when talking to a CTO from UK. I heard of RPC but not "gRPC", so i did a quick search on Google and found out that it works similarly to Microsoft WCF, just that it supports different programming languages ranging from C++, C#, Ruby, Python, etc and it's developed by Google. WCF supports only .NET languages. To find out more here's a link https://stackoverflow.com/questions/35694273/whats-the-difference-between-grpc-and-wcf

One more thing that i would like to mention about WCF is, there's a community in the.NET Foundation that has started the effort to create an open source WCF (Currently WCF Client in development, but not the WCF core itself) targeting for .NET Core programming model. However it's still at a very infant stage and probably not ready for production use out of the box, unless user development team is willing to take on the extra effort to customize the codes and fix any new issues discovered.

The link to the WCF .NET Foundation https://www.dotnetfoundation.org/blog/2015/05/20/wcf-is-open-source

In ASP.NET Core, Visual Studio uses the Protobuf compiler to compile the proto code into C# code.
gRPC Web UI - https://github.com/uw-labs/bloomrpc which ease the testing for the gRPC service contract.

Friday, December 20, 2019

Tip: Using matplotlib from Python in Visual C++

Recently when playing with OpenCV, I realized that I need to have a library that can display images with the scale that i can set. After some searching, I found that I can make use of the matplotlib library from Python in my C++ program. After further searching, I found that I can borrow the C++ library that call to the Python matplotlib library from the following GitHub Matplotlib

How the C++ library can make use of the Python matplotlib package is by using the Python compiled library (Python36.lib) and referencing the Python.h header file, which I got it from the Python installation path at C:\Program Files (x86)\Python3.6.3\libs. If the file you are an invalid lib file or dll file, most probably you will see the following error.
In order to get rid of this error, I built the application with 32-bit release version of python library.


I have to ensure that I have matplotlib installed for Python. Note that I have multiple versions of the Python installed in my PC and I need to install the matplotlib package just for Python3.6.3 because I am going to use that Python version in my C++ project. I used the following command to install the matplotlib package just for Python 3.6.3

C:\Program Files (x86)\Python3.6.3>python -m pip install matplotlib.

Basically I CD the Python installation path so that when i call the Python command it will call to the 3.6.3 version, as my default Python installation at that time was 3.7.3, which is not what i want to use in my C++ project.

Note: If you are using python.exe, that means the numpy and matplotlib packages need to be compatible with the "Release" version. If you are using python_d.exe then you need to ensure numpy and matplotlib is compatible with the "Debug" version. For debug version you can use C:\Program Files (x86)\Python3.6.3>python_d.exe -m pip install matplotlib to do correct package installation. Typical error one will get when the version is not compatible is ImportError: cannot import name 'multiarray'
A good guideline for using debug version of the Python with release version of matplotlib from within Visual C++ Numpy issue

The full source code for the release can be retrieved from GitHub https://github.com/leonwooster/matplotlib

Sunday, October 20, 2019

Tip: Web API vs WCF - High Level

Location Concern
Within the company Network and .NET based clients : Use WCF with TCP binding (Fast communication than HTTP)
Outside the company Network, and use diverse technologies like PHP, Python, web UI etc: Use Web API with REST

Security Concern
WCF support WS- standard and SOAP protocal.
Web API is tighly coupled with http, so can only use http releated security measures.

Network languages
WCF support multiple protocols like SOAP, TCP, HTTP, HTTPS, Named Pipe, MSMQ,
Web API supports only http, https

Architecture
WCF can be used to build SOA compliance services due to the nature that it's able to support different protocols.
Web API is mainly used for front end integration, e.g. Web UI because of http protocol.


Message Format
WCF supports more message formats than web api, e.g. binary message formats
Web api supports text based message format e.g. JSON, XML

Speed
WCF offers faster speed than web api when the service and client sits in the same server. We can use tcp protocol to achieve fast message transfer.

Wednesday, October 9, 2019

Tip: How to add form data as a parameter(s) into Swagger UI

Recently I come across the need to pass form data from an Angular form to a web API written in ASP.NET MVC. The web api does not accept any input parameter specifically via the function signature. However i need to access some form values via the HttpContext.Current.Request.Form. Unfortunately the form does not provide any text box to enter the necessary payload.

After doing some research I found out how to make Swagger to show an extra text box just for that particular web api. First of all I need to define a function attribute that i can use for the web api, and also to tell Swagger that the function actually consist of extra parameters that need to shown in the UI. The code looks like the following:

The next piece of the code shows how we can register the attribute filter to the Swagger. The code tells Swagger to go through every Controller class in the project and check for the specified filter and in turn inform the Swagger UI about any extra parameters to create.
As you can see during application startup, the Swagger will trigger this code via the following configuration in SwaggerConfig.cs file.
The following code shows how the attributed is applied:








The picture below shows the end result in Swagger UI:





Thursday, September 26, 2019

Tip: SharePoint 365 CSOM Nuget Package

Which on to use for SharePoint Online, when you want to use CSOM to connect to it?

NOT To Use


To Use

The reason for this post is to let people know that whenever you someone try to execute some operations to SharePoint Online and receive the following error, when your codes seem to be correct:
Microsoft.SharePoint.Client.CollectionNotInitializedException HResult=0x80131509 Message=The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. Source=Microsoft.SharePoint.Client.Runtime
The are two reasons for such scenario:

  1. Try to access some properties within the CSOM object namespace but did not execute the proper "Load" and "ExecuteQuery" functions. 
  2. Used the wrong library version. As indicated above the package shown in first picture is for SharePoint2010 whereas the second picture is for SharePoint Online.