📜 ⬆️ ⬇️

We are testing IBM cloud platform on the example of chat


Recently, I happened to meet with IBM technical specialist, who told me about the capabilities of the relatively new IBM Bluemix platform, which should make life easier for the developer. Using the example of a simple chat, I decided to check this statement.



Prehistory


Previously, my experience with the platform was close to zero, so in order to understand the huge number of platform services, I again turned to my friend from IBM. It seemed obvious to me to use the Message Hub service, but in the end it turned out that MQ Light would be better suited for solving our problem. Main reasons:



We also considered the option of using the IoT Foundation service, but as it turned out, devices cannot subscribe to receive any other events except commands, and they cannot send commands themselves either. It turns out, we can send data as we want, and receive only in the form of commands.
')
So, let's go to the IBM Bluemix Dashboard.

We register cloud service


Since we don’t need any server-side wrappers for the chat, we just need to register the service and get the connection data.

Go to the panel, click “Use services or APIs”:


Looking for MQ Light:


And we take one such myself.


We return to the Dashboard, select MQ Light - Service Credentials. We create data for authorization of our applications.


We get:

Check in advance the presence of ";" in the login and password. The compiler will not like it very much, it is better to generate a new pair.

Create a desktop application


Since we need to quickly create a working application - use Node.JS and install the MQ Light library.
npm install mqlight 


... and open our development environment. The first step is to connect the library:
 var mqlight = require('mqlight'); 


Now, to create realtime chat, we have to work with data entry from the console. For this, the standard library from the Node.JS - readline set is perfect.
 var readline = require('readline'); 


Add some variables for MQ Light.
 var TOPIC = "mqlight/simplechat"; var SHARE_ID = ""; var opts = { service: 'connectionLookupURI', //    user: 'username', password: 'password' }; 


And do not forget about the input / output:
 var rl = readline.createInterface(process.stdin, process.stdout); 


Next, when starting, ask for a nickname and subscribe to the channel with a nickname in share id:
Hidden text
 mqlightClient.on('started', function() { rl.question("Please, choose your nickname: ", function(result) { SHARE_ID = result; console.log("Welcome,", SHARE_ID, "!"); rl.setPrompt(SHARE_ID + '> '); mqlightClient.subscribe(TOPIC, SHARE_ID, { credit : 5, autoConfirm : true, qos : 0}, function(err) { if (err) console.error("Failed to subscribe: " + err); else { console.log("Subscribed to " + TOPIC); rl.prompt(); } }); }); }); 


And we process the receipt of the message (the event 'message' in MQ Light) and the sending of the message (we catch the event 'line' in the readline):
Hidden text
 mqlightClient.on('message', function(data, delivery) { data = JSON.parse(data); if (data.id != SHARE_ID) { readline.clearLine(process.stdout, 0); rl.setPrompt(data.id + '> '); rl.prompt(); console.log(data.message + ' '); rl.setPrompt(SHARE_ID + '> '); rl.prompt(); } }); rl.on('line', function(message) { if(message != "") { var data = JSON.stringify({ id: SHARE_ID, message: message }); mqlightClient.send(TOPIC, data, { ttl: 60*60*1000 }); rl.prompt(); } }); 


The application is ready. At startup, it will try to connect to the IBM servers, then ask the nickname, and subscribe to the / mqlight / simplechat channel with the nickname as the SHARE ID. All it took was just 64 lines! It seems a very good result for one evening.

Testing


Check if everything works:


Profit! And this is how it looks in the control panel:


Conclusion


Now about the results.


So, we solved the task: to create a chat with minimal resources, in just one evening. With IBM Bluemix, you can quickly create applications of any complexity using a variety of ready-made services and APIs. Well, cloud technologies are evolving, and in the future the work of a programmer can become much simpler and more interesting, but what do you think?

Source: https://habr.com/ru/post/272443/


All Articles