IBM Worklight 5.0.6 - /init vs WL.Client.connect() call - ibm-mobilefirst

I have a simple test application where I am deciding how to handle the server connection via WL.Client.connect().
During the application startup no authentication process takes place. Just the standard initialization with initOptions.js (connectOnStartup:false) and then the wlCommonInit() invocation where I have placed the call to the server (WL.Client.connect()) with proper success/failure handlers.
When I test the app with the Mobile Browser Simulator (e.g.: Android environment), I got the following stack trace from the browser console:
WL.Client.init() passed! wlgap.android.js:1538
wlclient init started wlgap.android.js:1538
before: app init onSuccess wlgap.android.js:1538
Request [/apps/services/api/UnisTestAdapters/android/init] wlgap.android.js:1538
inside wlCommonInit wlgap.android.js:1538
after: app init onSuccess wlgap.android.js:1538
wlclient init success wlgap.android.js:1538
wlclient init started wlgap.android.js:1538
before: app init onSuccess wlgap.android.js:1538
Cannot invoke WL.Client.connect while it is already executing. wlgap.android.js:1544
inside wlCommonInit wlgap.android.js:1538
Cannot invoke WL.Client.connect while it is already executing. wlgap.android.js:1544
inside wlCommonInit wlgap.android.js:1538
after: app init onSuccess wlgap.android.js:1538
wlclient init success wlgap.android.js:1538
Failed to load resource: the server responded with a status of 401 (Unauthorized) http://<myhost>/apps/services/api/UnisTestAdapters/android/init
Request [/apps/services/api/UnisTestAdapters/android/init] wlgap.android.js:1538
response [/apps/services/api/UnisTestAdapters/android/init] success: /*-secure-
{"userPrefs":{},"WL-Authentication-Success":{"wl_remoteDisableRealm":{"userId":"null","attributes":{},"isUserAuthenticated":1,"displayName":"null"},"wl_antiXSRFRealm":{"userId":"2lpeuqgs32jqt15bkbu0hg19j","attributes":{},"isUserAuthenticated":1,"displayName":"2lpeuqgs32jqt15bkbu0hg19j"},"wl_deviceNoProvisioningRealm":{"userId":"previewDummyId","attributes":{"mobileClientData":"com.worklight.core.auth.impl.MobileClientData#15c4c586"},"isUserAuthenticated":1,"displayName":"previewDummyId"},"wl_anonymousUserRealm":{"userId":"a940a5a8-7506-4052-9445-87b8aeeb23f9","attributes":{},"isUserAuthenticated":1,"displayName":"a940a5a8-7506-4052-9445-87b8aeeb23f9"}},"notificationSubscriptionState":{},"gadgetProps":{"ENVIRONMENT":"android"},"userInfo":{"wl_authenticityRealm":{"userId":null,"attributes":{},"isUserAuthenticated":0,"displayName":null},"UnisaluteAuthRealm":{"userId":null,"attributes":{},"isUserAuthenticated":0,"displayName":null},"SampleAppRealm":{"userId":null,"attributes":{},"isUserAuthenticated":0,"displayName":null},"wl_remoteDisableRealm":{"userId":"null","attributes":{},"isUserAuthenticated":1,"displayName":"null"},"wl_antiXSRFRealm":{"userId":"2lpeuqgs32jqt15bkbu0hg19j","attributes":{},"isUserAuthenticated":1,"displayName":"2lpeuqgs32jqt15bkbu0hg19j"},"WorklightConsole":{"userId":null,"attributes":{},"isUserAuthenticated":0,"displayName":null},"wl_deviceAutoProvisioningRealm":{"userId":null,"attributes":{},"isUserAuthenticated":0,"displayName":null},"wl_deviceNoProvisioningRealm":{"userId":"previewDummyId","attributes":{"mobileClientData":"com.worklight.core.auth.impl.MobileClientData#15c4c586"},"isUserAuthenticated":1,"displayName":"previewDummyId"},"myserver":{"userId":"a940a5a8-7506-4052-9445-87b8aeeb23f9","attributes":{},"isUserAuthenticated":1,"displayName":"a940a5a8-7506-4052-9445-87b8aeeb23f9"},"wl_anonymousUserRealm":{"userId":"a940a5a8-7506-4052-9445-87b8aeeb23f9","attributes":{},"isUserAuthenticated":1,"displayName":"a940a5a8-7506-4052-9445-87b8aeeb23f9"}}}*/ wlgap.android.js:1538
wlclient connect success
Request [/apps/services/api/UnisTestAdapters/android/heartbeat] wlgap.android.js:1538
response [/apps/services/api/UnisTestAdapters/android/heartbeat] success: wlgap.android.js:1538
Request [/apps/services/api/UnisTestAdapters/android/heartbeat] wlgap.android.js:1538
response [/apps/services/api/UnisTestAdapters/android/heartbeat] success: wlgap.android.js:1538
Request [/apps/services/api/UnisTestAdapters/android/heartbeat] wlgap.android.js:1538
response [/apps/services/api/UnisTestAdapters/android/heartbeat] success: wlgap.android.js:1538
Request [/apps/services/api/UnisTestAdapters/android/heartbeat] wlgap.android.js:1538
response [/apps/services/api/UnisTestAdapters/android/heartbeat] success: wlgap.android.js:1538
It results that the wlClientInit is called 3 times, until the call to /apps/services/api/UnisTestAdapters/android/init gets a response from the server.
In the while, the call to WL.Client.connect() fails twice because Cannot invoke WL.Client.connect while it is already executing. Then after getting the response from the /init call I also get wlclient connect success.
Given this scenario, I have some questions:
Is the /init call a WL.Client.connect() under-the-covers call? Do both the server invocations carry the same information in the response? In other words, does the /init call act as a WL.Client.connect() ensuring that all the features that need a connect() call are likewise available?
Am I guaranteed by the "retry mechanism" that calls the wlClientInit() until the /init call is terminated allowing the successful call to WL.Client.connect()? Is there any way to prevent the WL.Client.connect() call to fail twice before being successful?
Can you confirm, as I see in the console, that there's a default heartbeat set to prevent the session from timing out? What is that default heartbeat interval?
#Daniel Gonzales: here it is the wlCommonInit() code and related handlers:
function wlCommonInit(){
// Common initialization code goes here
busyind = new WL.BusyIndicator("content");
$('#SubscribeButton').bind('click', subscribeButtonClicked);
$('#UnsubscribeButton').bind('click', unsubscribeButtonClicked);
WL.Client.connect({ onSuccess: connected,
onFailure: notconnected,
timeout: 1000
});
WL.Logger.debug("inside wlCommonInit");
}
function connected(response){
alert("connected");
}
function notconnected(response){
alert("not connected");
}

See these two questions (well, their answers) for some explanations that may help.
IBM Worklight - Connecting/Re-Connecting: WL.Client.connect vs. connectOnStartup vs. WL.Client.invokeProcedure
Worklight method for checking connection
As for Heartbeat, yes, it is enabled by default and has a default of 20 minutes.

Related

[Karate Test][Saucelab]: Session disconnecting after launching mobile app installed in Saucelab

I am launching android test on Saucelab, however I can see the script is launching the mobile app on saucelab but not able to perform any action on it and throwing this exception:
12:54:20.931 [main] ERROR com.intuit.karate - java.net.SocketTimeoutException: Read timed out, http call failed after 32303 milliseconds for url: https://oauth-abdulkadir786-684f1:1bd00f8e-392f-4b4c-8f0e-2597cc9912a3#ondemand.eu-central-1.saucelabs.com:443/wd/hub/session
I am using the following steps for execute my test:
Configure driver in karate-config.js
var android = {}
android["desiredConfig"] = {
"accessKey":"1bd00f8e-392f-4b4c-8f0e-2597cc9912a3",
"deviceName":"Android GoogleAPI Emulator",
"app" : "storage:b82d6099-60ad-49dd-b15f-925166e03dcd",
"platformVersion" : "11.0",
"platformName" : "Android",
"newCommandTimeout":300,
"automationName" : "UiAutomator2",
"username": "oauth-abdulkadir786-684f1"
}
config["android"] = android
Then Writing the feature file to call the webDriverSession:
Feature: Calling test from Sauce labs
Background:
* configure driver = { type: 'android', start: false, webDriverUrl: 'https://oauth-abdulkadir786-684f1:1bd00f8e-392f-4b4c-8f0e-2597cc9912a3#ondemand.eu-central-1.saucelabs.com:443/wd/hub' }
Scenario: android mobile app UI tests
Given driver { webDriverSession: { desiredCapabilities : "#(android.desiredConfig)"} }
* delay(2000)
Then click('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[3]')
Any help would be highly appreciated.....
It looks like Karate is timing out the session before the emulator and app gets a chance to load fully:
Read timed out, http call failed after 32303 milliseconds for url: https://oauth-abdulkadir786-684f1:1bd00f8e-392f-4b4c-8f0e-2597cc9912a3#ondemand.eu-central-1.saucelabs.com:443/wd/hub/session
The suspicious parts are Read timed out, 32303 milliseconds (which is pretty close to 30 seconds and so is probably a config thing; 30 or 60 seconds is a common default timeout) and the path /wd/hub/session looks like the initial POST request which starts a sesson.
You'll probably have more luck if you increase the connectTimeout and readTimeout, either in your Background or in karate-config.js:
karate.configure('connectTimeout', 60000);
karate.configure('readTimeout', 60000);

webpack dev-server: Avoid proxy errors on HTTP errors returned from proxy target

I have a Vue.js project where I have configured a webpack dev-server to proxy all requests to the UI to my backend server. Here is the relevant part of vue.config.js:
devServer: {
contentBase: PATHS.build,
port: 9000,
https: false,
hot: true,
progress: true,
inline: true,
watchContentBase: true,
proxy: {
'^/': {
target: 'http://127.0.0.1:8089',
secure: false
},
}
},
I've noticed that if the HTTP response code from http://127.0.0.1:8089 is anything other than 2xx then the proxy fails with the following error:
Proxy error: Could not proxy request /api/test from localhost:9000 to http://127.0.0.1:8089.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (HPE_INVALID_CHUNK_SIZE).
This also causes the HTTP response code from the request to localhost:9000 to be 500 for any error and all the information about what went wrong on the server side is lost. This is problematic as I want to be able to extract information from error responses to display to the user.
I know it's possible to do because I had it working on an older Angular project which I think was using Webpack 3 (am now using Webpack 4). I tried copying all the dev-server config from this project but it just doesn't seem to work here!
EDIT: I was wrong. The Proxy error does not occur on every bad response but only for one of the requests which is a multipart file upload. Still unable to reproduce this in a smaller example to put on github though so struggling to pinpoint the cause.
This error message comes from node_modules/#vue/cli-service/lib/util/prepareProxy.js, which define a onError callback for node-http-proxy;
So I did some experiment, make back-end api generate 400 404 500 response, but I didn't got this error.
After I happen to close back-end api, error arise:
Proxy error: Could not proxy request /hello from localhost:8080 to http://localhost:8081 (ECONNREFUSED).
I search in the doc and find these:
The error event is emitted if the request to the target fail. We do not do any error handling of messages passed between client and proxy, and messages passed between proxy and target, so it is recommended that you listen on errors and handle them
So the onError do not handle error code, is called only when request fail (500 response is still treated as a complete request, connection refuse is not)
Go back to your error message, [HPE_INVALID_CHUNK_SIZE] means bad request to the back-end api. In this issue, it gives an solution: add a keep-alive header:
devServer: {
publicPath: 'http://localhost:9090/front/static-dev/build/',
port: 9090,
proxy: {
'/**': {
target: 'http://localhost:8080',
secure: false,
changeOrigin: true,
headers: {
Connection: 'keep-alive'
}
},
open: true
}
I have finally found the problem, and I apologise, it was a lot more of a specific issue than I originally thought when I wrote the question.
Issue was to do with a request which was proxied to another server using the Spring RestTemplate:
e.g.
#PostMapping("/upload")
public ResponseEntity upload(#RequestParam("file") MultipartFile file)
throws Exception {
String baseUrl = serviceProperties.getAddress();
HttpEntity<MultiValueMap<String, Object>> request = createMultipartRequest(file.getBytes());
return restTemplate.postForEntity(baseUrl + "/api/upload", filterRequest, String.class);
}
The ResponseEntity returning from the rest template proxy contained the header "Connection: close" when the response was anything other than 200 which cause the connection to close and caused this request to fail to return anything which subsequently made the dev-server proxy fail on the UI.
Fixed this by not passing the response headers from the rest template proxy to the response:
#PostMapping("/upload")
public ResponseEntity upload(#RequestParam("file") MultipartFile file)
throws Exception {
String baseUrl = serviceProperties.getAddress();
HttpEntity<MultiValueMap<String, Object>> request = createMultipartRequest(file.getBytes());
ResponseEntity response = restTemplate.postForEntity(baseUrl + "/api/upload", filterRequest, String.class);
return new ResponseEntity<>(response.getBody(), response.getStatusCode());
}

tokbox : Subscriber time out error on Safari

Using opentok.js v2,  the video channel works fine with Chrome and Firefox . The opentok version used is from this link: https://static.opentok.com/v2/js/opentok.min.js
But it doesn't work with safari 11.0.3.
On session's stream created event, it generates following error message "The stream was unable to connect due to a network error. Make sure your connection isnt blocked by a firewall"
The publisher is published successfully i.e session.publish(..) works fine.
The code below is used to make a video call:
this.session=OT.initSession(this.apiKey, this.sessionId);
this.session.on({
streamCreated: (event) => {
 
this.session.subscribe(event.stream, 'subscriber');
},
streamDestroyed: (event) => {
console.log(`Stream ${event.stream.name} ended because ${event.reason}`);
}
});
this.session.connect(this.token, () => {
this.publisher=OT.initPublisher('publisher');
this.session.publish(this.publisher);
});
}
 
Other exceptions in console generated by opentok.js are as follows: 
[Error] OT.exception :: title: undefined (1554) msg: OT.Subscriber PeerConnection Error: OT.Subscriber failed to subscribe to a stream in a reasonable amount of time
error
_exceptionHandler (vendor.js:150924)
handleJsException (vendor.js:151002)
onPeerConnectionFailure (vendor.js:162673)
(anonymous function) (vendor.js:162414)
onInvokeTask (vendor.js:4239)
runTask (polyfills.js:3:10225)
invokeTask (polyfills.js:3:16182)
n (polyfills.js:2:31400)
[Error] OT_ICE_WORKFLOW_FAILED: ICEWorkflow: Subscriber PeerConnection with connection (not found) failed: OT.Subscriber failed to subscribe to a stream in a reasonable amount of time
error
dispatchOTError (vendor.js:159161)
(anonymous function) (vendor.js:160080)
handleThisOnce (vendor.js:137437)
(anonymous function) (vendor.js:137675)
onInvokeTask (vendor.js:4239)
runTask (polyfills.js:3:10225)
invokeTask (polyfills.js:3:16182)
n (polyfills.js:2:31400)
[Error] OT.exception :: title: undefined (1554) msg: ICEWorkflow: Subscriber PeerConnection with connection (not found) failed: OT.Subscriber failed to subscribe to a stream in a reasonable amount of time
error
_exceptionHandler (vendor.js:150924)
handleJsException (vendor.js:151002)
dispatchOTError (vendor.js:159163)
(anonymous function) (vendor.js:160080)
handleThisOnce (vendor.js:137437)
(anonymous function) (vendor.js:137675)
onInvokeTask (vendor.js:4239)
runTask (polyfills.js:3:10225)
invokeTask (polyfills.js:3:16182)
n (polyfills.js:2:31400)
 
 
All the above errors  are generated only on Safari browser. 
You need to make sure you have created a Safari Project in your OpenTok Account Portal. More details at https://tokbox.com/developer/sdks/js/safari/
It looks like you're using a polyfills.js file. If that is the polyfill from Angular that includes zone.js then you will need to include the fix for RTCPeerConnections 'zone.js/dist/webapis-rtc-peer-connection' in your polyfills.js file.
import 'zone.js/dist/webapis-rtc-peer-connection';
You will also probably want to include the polyfill for getUserMedia. More details at https://github.com/angular/zone.js/issues/948#issuecomment-357558384

Sailsjs socketio http status

I know I can call REST API of sails using socket.io. And return me the response. Following is a simple way to do that
socket.get("/", function (response) { console.log(response); })
But I also want the http status code along with response how I can get that?
If you're using the API blueprints, then the response will return the status code in the event of an error. For example, if there was a general server error, you'll get back:
{status: 500}
Otherwise, you'll get data in the response and you can assume the status was 200.
If you're using a custom controller action, then you can use any of the default responses (like res.serverError(), res.forbidden(), etc) to send back a status code, or you can set one yourself:
myAction: function (req, res) {
return res.forbidden(); // Will send {status: 403}
// OR
return res.json({status:400, error: 'Bad request!'})
}
But if you just send the status using res.json(500, {error: 'someError'}), you won't be able to retrieve it on the client.
Update
On Sails v0.10.x, using the new Sails socket client library, the request methods (io.socket.get, io.socket.post, etc) have callbacks that accept two arguments: the first being the response body (equivalent to the response in the previous client library version), and the second being an expanded response object which includes the status code, headers and more.

Dojo JsonRestStore, fetch, onError callback and HTTP status code?

I would like to know how to obtain the HTTP status code returned after a fetch() operation is performed. I have specified the onComplete and onError callbacks to the fetch() call.
The onError is called in case of an error, but I am unable to obtain the HTTP status code from the parameter passed to onError (it's just the request, not the response).
With XhrGet I was able to get the status code from the ioArgs, and it seems that the JsonRestStore does not handle it that way.
I'm using Dojo 1.5.1 (and I really cannot upgrade yet to 1.6)
The error handler is given two arguments. The second argument (which I called config) has a property called xhr which contains... status and status text.
dojo.xhrGet({
url:'/bogusPath/',
error:function(error, config){
console.log('XHR-ErrorHandle',arguments);
console.log('XHR-ErrorHandle-status:',config.xhr.status);
console.log('XHR-ErrorHandle-statusText:',config.xhr.statusText);
}
})
Returns:
XHR-ErrorHandle [Error, Object]
XHR-ErrorHandle-status: 404
XHR-ErrorHandle-statusText: Not Found