Introduction
To develop a client application in Javascript, you must previously have the following JS files in the application directory:
- jQuery
- Stomp
- Sockjs
- op_ws_client
The images shown in the tutorial use an additional file called JsonEditor.js to show the response more esthetically, but it is not strictly needed.
For starters, you must instantiate an OPClient type object to create a OnesaitPlatform client:
var client = new OPClient();
Next, you need to create an object where you will store the config parameters (In this case, those of the selected example):
var config = {};
- config['url'] = "http://www.onesaitplatform.online/iot-broker/message";
- config['deviceTemplate'] = 'Ticketing App'; (Must be the same as the template you defined in IoTClient)
- config['token'] = 'e7ef0742d09d4de5a3687f0cfdf7f626'; (deviceTemplate token)
- config['device'] = 'Web'; (Name you will give to the device)
This variable will be passed as a parameter to the previously defined client object using the configure() method:
client.configure(config);
Lastly, you must connect with the platform using the connect method:
client.connect();
Once this step is done, you can invoke all the methods that are implemented in the OPClient object:
- Join
- Query
- Insert
- Update
- UpdateById
- Remove
- RemoveById
- Subscriptions
JOIN
The Join operation will be the first action to be made, because it allows you to connect to the defined IoTClient. If everything is OK, it will return the sessionKey, identifying the current session.
QUERY
The Query operation allows to get the data that were stored and associated to a given ontology. You must specify the ontology associated to the IoTClient/deviceTemplate in the ControlPanel.
Both Native and SQL queries are accepted. You can also perform SQL queries including SELECT/UPDATE/DELETE.
Native
SQL
TO-DO (EXAMPLE UPDATE/DELETE)
INSERT
The Insert operation allows you to insert new data into the ontology, make sure the data has the structure defined in the ontology JSON Schema, otherwise it won't pass Schema validation. To ensure that the format is correct, the best choice is copying it directly from the Ontology Schema. It returns the inserted instance's id.
It's important to note that you can pass to the function an array of Ontology instances as data, for example:
This operation will return the ID (or IDs in case data it's an array), which you can use later to perform operations 'by id'.
Update
To update an existing instance, you can either update the whole instance or update selected fields.
In the first case you may want to use 'updateById'. It works similar to insert function.
For the second case, you may use 'update' function. This function only accepts Native queries. If you want to make SQL Updates, make use of the query function.
DELETE
If you want to delete a single instance, you may use 'removeById'.
On the other hand, if you want to delete all instances matching a particular filter, use 'remove' function. This function, as the update one, only accepts native queries.
SUBSCRIPTIONS
This API allows you to subscribe to an Ontology. When subscribed, any insert/update related to the subscribed ontology, will notify your client, resulting in an execution of your callback function passed to handle subscription notifications.
Upon subscribing you will receive a subscriptionId in the callback function, which you can user later to filter arriving subscription messages, for example.
Take a look at the subscribe function:
The first callback function will return as response the message with the subscription ID associated.
The second callback function will be executed each time a subscription notification/message arrives.
If you click in the example on 'subscribe', and then perform and insertion, if you go back to the subscribe tab, you will se that you received the instance you just inserted.
Lastly, you can download those files mentioned in the beginning in the following link, also including an example html page to perform tests: Prueba ClientJS.zip