Websocket does not work in Ionic - ionic-framework

What should I do in order to use websocket in my ionic app?
I couldn't find any info in docs about this.
I'm just writing in my code:
.controller('Controller', function($scope) {
$scope.planets = [{'name': 'Меркурий', 'order': '1'},
{'name': 'Венера', 'order': '2'},
{'name': 'Земля', 'order': '3'},
{'name': 'Марс', 'order': '4'},
{'name': 'Юпитер', 'order': '5'},
{'name': 'Сатурн', 'order': '6'},
{'name': 'Уран', 'order': '7'},
{'name': 'Нептун', 'order': '8'},
{'name': 'Плутон', 'order': '9'},
];
$scope.sendData = function(selected) {
var planets = $scope.planets;
var num;
for (var i=0; i < planets.length; i++) {
if (planets[i].name === selected) {
num = planets[i].order;
}
}
var socket = new WebSocket("ws://somesource.com");
socket.send(num);
socket.onmessage = function(data) {
alert(data);
};
};
})
And nothing...
By design somesource.com must return me some data responding on my "num".

It's likely you would have found something if you had searched for WebSockets on Cordova instead of Ionic (Cordova is an underlying component of Ionic).
WebSockets are not officially supported because Android has no native WebSocket support and only the newest iOS devices have native WebSocket support:
WebSockets or an alternative with phonegap?
https://www.quora.com/Does-PhoneGap-support-WebSocket
As an alternative, I recommend following this tutorial to learn how to use Socket.IO with Ionic:
https://www.sitepoint.com/using-socket-io-and-cordova-to-create-a-real-time-chat-app/

Related

how to Send push notifications from Flutter app

I use firebase messaging to push notifications.
Now i push notifications from console to users on app, but I want the user to push notifications from app to another user when send message. How can I do that?
Knowing that I have saved the device ID of each user on Firestore
and the ID is renewed every time the user logs into the application.
I tried using the following code to send notifications in case the user sends a message, but it didn't work
sendNotification(String body, String idToken) async {
await http.post(
(Uri.parse('https://fcm.googleapis.com/fcm/send')),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$servitoken',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': "${body.toString()}",
'title': "!"
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': await "${idToken}",
},
),
);
}
,##
I will recommend you to use onesignal instead of just using firebase cloud messaging. Onesignal is easy to use and can make your development life more

500 error when trying to create page using REST API

I'm currently using Confluence server and I'm currently getting a 500 error when I try to create a new page using the REST API. I am currently using an HTML macro that makes GET & POST requests using the Fetch API. I currently have no issues when making GET requests, only issues with POST requests.
I tried researching the error and saw someone mention that they fixed it by turning off collaborative editing in the space, but in my case that is not an option. Anyone have an idea of what is causing this error?
function createPage() {
let url = "http://exampledomain:8090/confluence/rest/api/content/"
fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Basic USER:PASS',
'Content-Type': 'application/json',
},
data: {
'type': 'page',
'title': "New Page",
'ancestors': [{ 'id': 97059352 }], // parent page id
'space': { 'key': "EXAMPLE_SPACE" },
'body': {
'storage': {
'value': "<h1>New Page!</h1>",
'representation': 'storage',
}
},
}
})
.then(response => console.log(response))
.catch(err => console.log(err.message))
}
I see invalid data structure:
'representation': 'storage', <== extra comma
}
}, <== another extra comma
}
Also double check with your programming language that you can use single quotes (') and that they are correctly transformed into double quotes ("). JSON (Jira REST API) accepts only double quotes for keys and string values.

Drupal 8-9 REST translations

I can't send PATCH request to my REST API to send node with translations.
The error is 422 Unprocessable Entity: validation failed. title: Title: this field cannot hold more than 1 values.
I was trying JSON:API and REST, but the only way I found is to manually create translations, and after that update each translation separately.
The data I'm trying to PATCH to REST is
const data = {
'type': 'charset',
'title': [
{ 'value': sName1, 'lang': 'en' },
{ 'value': sName2, 'lang': 'ru' },
],
'field_unicode_range': [{ 'from': iFrom, 'to': iTo }],
};
The JSON:API does not seem to currently support translations (Nov 2021).
"does support PATCH requests on translations: updating translations is possible" source.
There does seem to be some work going on around this is this issue

how do i iterate a cursor in pymongo

i am currently working on a project and i have created two collections in my mongodb .
i want to be able to get a particular field out of the collection
here is the code
app = mongo.db.apps
find_app = app.find({'email':session['email']})
x = []
for apps in find_app:
x.append(apps)
print(x)
this prints [{'_id': ObjectId('5bb07458e58fb90fc8a60d49'), 'email': 'celviuks#ge.com', 'app_name': 'apply', 'version': '1'},
{'_id': ObjectId('5bb0795ae58fb90c18bf7f5e'), 'email': 'celvius#egg.com', 'app_name': 'egg', 'version': '1'}]
and i want only the app_name field
please how to i achieve this?
i solved it with less code
app = mongo.db.apps
find_app = app.find({'email':session['email']}, {app_name: 1, _id: 0})
done_apps = [doc['appname'] for doc in find_app]
return render_template('somepage.html',done_apps=done_apps)
Try this:
app = mongo.db.apps
find_app = app.find({'email':session['email']},
{app_name: 1, _id: 0})
x = []
for apps in find_app:
x.append(apps)
print(x)

cordova background geolocation plugin on reboot

I am building an ionic app with background geolocation plugin https://github.com/mauron85/cordova-plugin-background-geolocation.
I want to make an app to send its location after reboot. The plugin I am using seems to have the option, but it is not working properly. An app only sends its location to the server only after execute an app at least once after every boot.
Any help or suggestion would be appreciated. Thank you in advance!
My code is below
Configuration
backgroundGeolocation.configure(callbackFn, failureFn, {
locationProvider: backgroundGeolocation.provider.ANDROID_ACTIVITY_PROVIDER,
desiredAccuracy: 10,
stationaryRadius: 10,
distanceFilter: 10,
interval: 60000,
maxLocations: 50,
startOnBoot: true, // from my understanding, this should make an app track its location even after reboot
stopOnTerminate: false
});
Callback Function
var callbackFn = function(location) {
console.log('[js] BackgroundGeolocation callback: ' + location.latitude + ',' + location.longitude);
// Do your HTTP request here to POST location to your server.
var link = API_URL;
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$http({
method: 'POST',
url: link,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
params: {
'device': 'android',
},
data: {
lat:location.latitude,
lng:location.longitude,
}
}).success(function(data){
console.log(data);
}).error(function(data){
console.log(data);
});
backgroundGeolocation.finish();
};
i hope you would have found your answer by now, posting this might help others too.
don't expect your callback to be executed after the reboot, as the activity might be killed, instead use url option of the plugin to continue sending your location updates to the server.