Azure IOT Hub Rest API with Javascript - javascript

I would like to receive IOT Hub messages from an endpoint. With client side Javascript and REST.
I used this article https://msdn.microsoft.com/nl-nl/library/mt590786.aspx for creating the URL.
This is my code:
function readIOTHub()
{
$.getJson("https://MyIOTHub.azure-devices.net/devices/device1/messages/devicebound?api-version=2016-02-03", function(result)
{
alert(result);
});
}
But my Request is not receiving any messages.
Does someone know how to receive messages from IOT Hub, with Javascript REST?

I don't think this is currently possible, first because, from what I see, Azure IoT hub REST API does not issue CORS requests (i.e. they don't write in the CORS header access-control-allow-origin), so your JS client can't access it from within the browser.

You might want to take a look at the Node.js sdk for the IoT hubs, but then again this is in the context of Node.js.
If you are ok with node.js, it becomes much simpler.
Hope this helps!
Mert

Related

Communicating between cordova and a python server

I've been trying to make a cordova app get information from a python server. I am relatively new to JavaScript but I've been trying to connect using sockets, but I couldn't get them to communicate and I can't use API since cordova blocks cross domain APIs.
How can I get them to communicate?
First, run two servers in the same domain. And use proxy server.
Here's an example case.
If your major app is one of Python, set proxy as:
yourdomain.com/ -> Python server
yourdomain.com/elsewhere/ -> Cordova server
Or you could set cordova app as the major app.
Second, communicate between them via HTTP or socket. It also can be done sharing a temp file or database.
The issue is that your API Server does not respond using CORS. If you can setup your python server to respond using CORS (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). There would be no issues. If it will work with the chrome developer Console then it works with cordova.
If it you can not make that, I would suggest writing a Firebase Functions that becomes a man in the middle to the story.

I can't get data from a public API : Access-control-allow-origin missing

I want to get data from a public API:
https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=1500&CMC_PRO_API_KEY=...
I don't understand why I'm getting a CORS error
Here a really simple Fiddle JS to see the error : http://tpcg.io/51kVwI
Thank you for your help :)
From: https://coinmarketcap.com/api/documentation/v1/#section/Quick-Start-Guide
Note: Making HTTP requests on the client side with Javascript is currently prohibited through CORS configuration. This is to protect your API Key which should not be visible to users of your application so your API Key is not stolen. Secure your API Key by routing calls through your own backend service.
They have example code for sending the request through a server, such as Node.js.
There's no way to hide API Key in Javascript, that's why the endpoint didn't allow client side (Javascript) to call it. You will need to make the request from server side. There are several serverless options with free tier out there, AWS Lambda, Google Cloud Functions, Azure Functions.

HTTPS Post request in Ionic 3

I'm working on an Ionic 3 application which requires me to send encrypted data over internet to AWS lambda function. I have a API created for this lambda function and I'm able to send a post request to the lambda function using ionic's http plugin. But we can easily track the post requests send from the current configuration of my application in the Network logs.
So how can I send my data from app to AWS lambda function in a secured way?
I read about the SSLpinning in http plugin but is this the only way to approach the issue?
The problem with encrypting in the app is, that it's not that hard to make reverse enginering to see how it is made.
It's a little harder to crack if you get a key from the server at login (or daily) and use this for encrypting.
https://ionicframework.com/docs/native/aes256/
I am pretty sure you can find other pre-made modules. Else make your own. Hackers hate custom design.
And btw - https-communication to be sure it's actual is you the app is talking to.

Azure service hub automatically receive message in javascript

I'm very new in Azure. Is there any way to receive messages from queue/topic automatically instead of making some kind of cron mechanism which will be making requests for new messages? This kind of functionality is available in RabbitMQ client.
The question is a bit vague... In C# SDK there is OnMessage callback that you can sign up to, see docs and a full example in C#.
For a fully automated serverless way of handling messages, have a look at Azure Functions Service Bus Trigger. It's based on WebJobs SDK which you could use directly in self-hosted apps.

Javascript remote events?

Is it possible to have a web service notify web clients when something occurs via HTTP request/response? Right now my client has to poll the server using HTTP Requests at an interval to check for updates, but it would be far more convenient if I could register a javascript function to the server from my client and have it be simply called when server state changes.
Note that the web service is written in Python and utilizes HTTP APIs (I think it uses cherrypy, if that's relevant).
If this is possible could someone point me to some tutorial that explains how to do this or give me a basic understanding of how this can be accomplished?
Look into Comet
You can't start a request from the server to the client. You can only poll the server (which you did), use a hidden iframe, use a plugin, or use the new HTML5 WebSockets which allows the server to send a message to the client.
If you are into Node.JS, look into socket.io and hook.io
https://github.com/hookio
https://socket.io

Categories

Resources