How To Build Securely Chatting Microservices With Flogo And PubNub
Embark on a secure microservices journey with Flogo and PubNub! đ Learn step-by-step how to build, connect, and safeguard your microservices in this insightful blog. Plus, a glimpse into the powerful Flogo Web UI for visual microservice development.
The cute mascot, Flynn
Building microservices is awesome, having them talk to each other is even more awesome! But in todayâs world, you canât be too careful when it comes to sending sensitive data across the wire. Last week I was at PubNub for a Meetup where, together with Jordan Schuetz and Nicholas GreniĂ©, we spoke about cool things you can do with PubNub. One of them is using PubNub as a messaging layer to have your microservices, built with Flogo (duh), talk to each other in a secure way. In this blog, Iâll go over the steps to build those microservices and hook them up using PubNub.
What are we building?
During the Meetup, Jordan and Nicholas showed a very cool demo about using Typeform technology to build a simple mobile app to open your front door. The messages flowed from a mobile device, through PubNub, to Slack where the user could click a button to either open the door or not. In this blog weâll look at building a simple microservice to receive the same messages from PubNub and write that data to a file, to make sure that we keep a safe ledger of everyone coming in.
Getting a PubNub account
First things first, in order to do all of this youâll really need a PubNub account. Luckily, registration is very easy. Simply go to https://dashboard.pubnub.com/login and use âSIGN UPâ to create a new account. After signing up, use the big red button to create a new app (the name doesnât matter, and if you want you can change it later too). Now click on the newly created app and youâll see a new KeySet. The Publish and Subscriber key are quite important as they make sure you can connect to your PubNub account.
Step 1: Creating a key set
Building your app using a UI
To get started with Flogo, the only thing you need installed is Docker. Check out this link if you need a quick refresher how to get the Flogo Web UI running, or if this is the first time youâre looking at it. Once you have pulled the Web UI from Docker Hub and have it running, open a browser and go to http://localhost:3303/apps.
Step 2: Create a new app
From here, click âNewâ to create a new microservice and give your new app a name. Click âCreate a Flowâ to create a new flow and give it any name that you want. Now click on the flow you just created and to open the canvas where you can design your flow.
Out of the box, Flogo doesnât come with a trigger for PubNub so using the sweet SDK provided by the PubNub teamâââI built my own! To install that into your Web UI, click on the â+â icon on the left hand side of the screen:
Step 3: Adding a trigger
From there click on âInstall newâ and paste âhttps://github.com/retgits/flogo-components/trigger/pubnubsubscriber" into the input dialog to get this new trigger. After the installation is completed, you can click on âReceive PubNub messagesâ to add the trigger to your app.
The thing we want to store in a file is the message coming from PubNub. To do so, youâll need to create an Input parameter which you can do by clicking on the grey âInput Outputâ bar on your screen. For example, you can call the parameter âpubnubmessageâ, keep the type as âstringâ, and click save.
Now itâs time to configure the trigger to listen to messages coming from PubNub. To start, click on the trigger and a new dialog will open with a bunch of options. In this dialog youâll have to provide:
- publishKey: The key from PubNub (usually starts with pub-c)
- subscribeKey: The key from PubNub (usually starts with sub-c)
- channel: The channel on which messages will come (totally up to you to choose this)
After that, click on âMap to flow inputsâ to map the message from PubNub to the âpubnubmessageâ parameter we created earlier. The parameter will already be selected because it is the only one, so the only task left is to click â* messageâ in the Trigger Output section and âsaveâ to make sure everything is, wellâŠ, saved. You can click the little âXâ on the top-right (no, not your browserâŠ) to close the dialog window and go back to the flow.
Weâll add two activities to the flow. The first activity will log the message and the second one will store the data in a file. To add an activity click on the other large â+â sign:
Step 4: Adding activities
On the right-hand side of the screen a list with all the activities the Flogo Web UI knows about will appear. From the list you can pick the âLog Messageâ activity and click it to make sure it is added to the flow. As you hover over the newly added activity, a cog will appear and as you hover over that thing, a menu will appear to configure your activity. In this window you can configure the inputs of the âLog Messageâ activity using data from all other activities and triggers in your flow. Right now, we have only the incoming data from PubNub so select âmessageâ in the âActivity Inputsâ section and expand the âflow (flow)â section (by clicking on the little black caret) to be able to select the âpubnubmessageâ.
Step 5: Mapping data
Now hit the âsaveâ button and weâre done with this part!
Adding activities
Now we need to add a new activity to the Flogo Web UI to make sure you can add things to a file. On the main screen of your flow click âInstall new activityâ to get the same dialog as when installing the trigger.
Step 6: Adding new activities
In the dialog window you can paste âhttps://github.com/retgits/flogo-components/activity/writetofile". Once the activity is installed you can select it from the list to add it to your flow. Again, hover over the newly added activity and expand the menu to configure your activity. For this activity youâll have to configure all the parameters:
- Append: Should the content be appended to the file or not? In this case we want to, so type âtrueâ in the box
- Content: The content we want to add to the file. In this case it is the message from PubNub again, so expand the âflow (flow)â section (by clicking on the little black caret) and select the âpubnubmessageâ
- Create: Should the file be created if it doesnât exist? Well, yes, in this case that is probably a good idea so type âtrueâ in the box
- Filename: The name of the file you want to write the data to. In this case you can call the file whatever you want, like âvisitors.txtâ (please be sure to add the double quotes as you type in the box)
Click âsaveâ to save the data and return to the main screen of your flow. The completed flow will look like:
A completed flow!
Building an executable
Those were all the steps needed to design the flow, now letâs build an executable from it. On the main screen of your flow click on the â<â button on the top-left hand side of the screen. That will bring you back to your microservice and from here you can select âBuildâ and choose your operating system of choice. That will tell the Flogo Web UI to go build your microservice and give you a tiny executable in return (about 12MB).
Letâs test it!
You can run your microservice by either double-clicking it (on Windows) or using a terminal window to run your app (MacOS or Linux). If the app started successfully it will give you a message like: 2018â08â06 21:20:02.867 INFO [engine]âââReceived status [pubnub.PNConnectedCategory], this is expected for a subscribe, this means there is no error or issue whatsoever
To test the microservice you can use the PubNub debug console:
Testing from the PubNub console
In the âDefault Channelâ youâll have to type the same channel name as you configured in your Flogo app (MyChannel, in this example). You can click âADD CLIENTâ to create a new client which will be able to send and receive data. The cool thing that PubNub offers, is that you donât have to open any firewall ports to have the debug console and the microservice talk to each other. At the bottom of the page it will now say â{âtextâ:âEnter Message Hereâ}â, which is the message that you can send to your microservice. Either hit âSENDâ or perhaps replace the default message with something like â{âHelloâ:âWorldâ}â. After you click âSENDâ the exact message will appear in the window where your microservice is running
Oh!! A message in a terminal window
And in the log file that was created in the same location as your app
Shows up in Notepad too :D
PubNub gives you the ability to have your microservices talk to each other securely and with Flogo you can build those microservices easily. No matter if youâre a Go developer or someone who builds microservices visually (through a very cool Web UI), you can do it using Flogo! If youâre trying out Flogo and have any questions, feel free to join our Gitter channel, create an issue on GitHub or even drop me a note on Twitter. Iâd also love to get your feedback if you thought this was helpful (or not).