PhantomAppDevelopment / firebase-as3

Integrate Firebase Auth, Realtime Database and Storage in your Adobe AIR projects.
MIT License
55 stars 12 forks source link

Is it possible to implement onDisconnect method? #4

Closed yong2khoo88 closed 7 years ago

yong2khoo88 commented 8 years ago

In Javascript, it is possible to pre-define the function/codes to be executed on the server side through the onDisconnect event. (such as remove its content)

Is it possible to do so in AS3?

Thanks in advance.

agentphantom commented 8 years ago

Hi,

You can implement this on several ways. For example in AIR mobile I detect when the user leaves the chat screen, when that happens I close and dispose the URLStream.

You only need to do something like:

myURLStream.removeEventListener(ProgressEvent.PROGRESS, myFunction);

myFunction is the same function that receives the messages.

You can see a full implementation in the following file:

https://github.com/PhantomAppDevelopment/pizza-app/blob/master/src/chatScreens/ChatScreen.as#L239

In the previous example on the dispose() function I can do anything I want, such as cleaning up objects or sending a request to the server.

yong2khoo88 commented 8 years ago

Hi agentphantom, sorry i don't quite get it. The onDisconnect is "used to manage operations that will be run on the server when the clients disconnect". Here, what i mean is when the user suddenly loses the internet connection. The onDisconnect is used to 'pre-define' what actions to be executed on the database (such as removes the user ID) when the server detects that the user has been disconnected.

When you mention 'dispose()' function, how does it 'send a request to the server' when it is already disconnected (say he loses internet connection)? Or, How can other users tell when one of the users has lost the internet connection?

In javascript, the code is something like this:

var userRef = new Firebase('https://<demo>.firebaseio.com/presence/' + userid);
amOnline.on('value', function(snapshot) {
  if (snapshot.val()) {
    userRef.onDisconnect().remove(); //this line is able to remove his own node as soon as he is disconnected
    userRef.set(true);
  }
});
agentphantom commented 8 years ago

Hi,

Now I fully understand what you mean. Right now the REST API and by extension the AS3 API officially don't support this feature (unless you use an ANE).

I took a look at the JavaScript SDK and found out that the onDisconnect function sends a small request to the server with specific strings "cancel", "remove", "set".

Problem is, the JavaScript SDK is minified, and that's just an educated guess from what I could understand.

Maybe it can be ported for AS3, but I don't know enough JavaScript to debug it. If you wish you can give it a try with JavaScript, debug all the requests that are sent, paste them here and then I can port it to AS3.

yong2khoo88 commented 8 years ago

Thanks for your reply. To be honest, i have no idea how to debug this. The network tab on the browser shows nothing even though i debug through the codes line by line. Anyway, i will continue looking into this.

yong2khoo88 commented 8 years ago

I posted the question in stackoverflow. Meanwhile, i found an un-minified version of the firebase at here. Still in the attempts of understanding the magic behind...

agentphantom commented 8 years ago

Thanks!

I read the question and they have the right idea. Monitoring the outgoing traffic will do the trick. I believe that a network analyzer like Wireshark can do the job.

Meanwhile I will keep reading the JS file to see if I can find the answer there.

agentphantom commented 8 years ago

This block looks interesting:

onDisconnectPut:function(pathString, data, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("o", pathString, data, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"o", data:data, onComplete:opt_onComplete}); } }, onDisconnectMerge:function(pathString, data, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("om", pathString, data, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"om", data:data, onComplete:opt_onComplete}); } }, onDisconnectCancel:function(pathString, opt_onComplete) { if (this.connected_) { this.sendOnDisconnect_("oc", pathString, null, opt_onComplete); } else { this.onDisconnectRequestQueue_.push({pathString:pathString, action:"oc", data:null, onComplete:opt_onComplete}); }

yong2khoo88 commented 8 years ago

I finally saw something like this at the network tab: fb

it seems like web socket or sth.

And when i click on that, it shows: fb2

(the 'ns' is my firebase namespace).

I am not sure if the normal REST API through urlrequest is able to achieve this, as mentioned by one guy in the stackoverflow....

agentphantom commented 8 years ago

Thanks for sharing those pictures!

Maybe the 'v' parameter does something special.

URLStream behaves almost the same as a websocket, the difference is that all the headers and responses are abstracted so you have an easier time sending and receiving them.

Disregard the commend about URLRequest, that guy doesn't know what he's talking about. URLRequest just describes what you want to do, it doesn't do any net connection by itself.

My recommendation is to find a way to read the outgoing traffic, there lies the answer.

yong2khoo88 commented 8 years ago

Thank you for your reply. Allow me to spend more time on this before making a conclusion.

yong2khoo88 commented 7 years ago

wireshark

Sorry for the long delay. Finally i am trying to use wireshark to capture something... However, i don't i think i understand what is being captured, as attached.

The only recognize-able letters are .....firebaseio.com..... Did i miss out something in the wireshark?

agentphantom commented 7 years ago

Hi,

My bad! I did some research and Wireshark is to analyze traffic after it has been processed, that's why the output is encrypted.

I recommend to use Fiddler: http://www.telerik.com/fiddler

This one analyzes the HTTP and HTTPS requests before they are sent (similar to the ones you added here some days ago), but it says its more complete.

If you wish you can give it another go with Fiddler and report back your findings.

yong2khoo88 commented 7 years ago

Hi, I managed to install and try the Fiddler. But I have no idea which part of the tabs show the 'data transferred' over the network. For example, if i perform login to facebook, i expect my username and password to be captured through the fiddler right? But i see nothing meaningful... Correct me if i am wrong.

agentphantom commented 7 years ago

You can try these tutorials, they look very useful:

https://www.ibm.com/developerworks/community/blogs/f37f6f3c-d39f-49c9-884b-a1665b7e2ca3/entry/tutorial_how_to_capture_http_traffic_using_the_fiddler_web_debugging_proxy?lang=en

http://www.infragistics.com/community/blogs/anton_staykov/archive/2010/08/24/how-to-debug-your-application-http-protocol-using-fiddler.aspx

yong2khoo88 commented 7 years ago

Hi Phantom, I think i saw something. But before anything, could you please tell me how to connect to 'websocket' in AS3?

Say the end point is sth like: ..wss://s-usc1c-nss-129.firebaseio.com/.ws?... i don't think URLLoader or URLRequest would work.

agentphantom commented 7 years ago

HI,

I haven't tried this but it looks very complete and useful: https://github.com/theturtle32/AS3WebSocket

Seeing at the examples it is very similar to setup like an URLStream.

yong2khoo88 commented 7 years ago

Thanks for you reply. i actually tried that. but it throws some errors: Definition com.adobe.net.URI could not be found. Definition com.adobe.net.URIEncodingBitmap could not be found. Definition com.adobe.utils.StringUtil could not be found.

Anyway, this is what i found. i use websocket plugin from firefox instead, that enables me to view what has been sent to the firebase ws However, that seems to be sent over the websocket, but not throught database REST API?

yong2khoo88 commented 7 years ago

My Bad, i didn't include the as3corlib. Now seems i am able to 'connect'. I will further post after more testing.

agentphantom commented 7 years ago

Ohh, interesting.

It sends a nested JSON with different parameters:

t (type maybe?) = "d" (probably refers to disconnect d (a data object containing the payload) r (no idea) = 4 (maybe it is a priority of some sorts) a (action maybe) = "o" could be linked to a specific task such as on disconnected, I remember there are several onDisconnect variations. b (a nested object containing the data, no clue what b could stand for) p (path) = /allUser/3 d (data) = "Hey I'm disconnect"

With this new finding I can take another look at the unminified library.

I will update this comment if I find something new.

Thanks!

yong2khoo88 commented 7 years ago

Hi Phantom,

I think you are almost right (with those parameters). I have tested with the local machine and it seems working. The conclusion is, it is feasible. (a bit of trouble but still it works) I have to thank you for your insights provided. I can rest in peace now. lol

[you may close this post :)]

agentphantom commented 7 years ago

That's great!

I will experiment with it and add it to the guide in the coming weeks.