a query related to firebase presence app using javascript sdk. I have seen that there is 60 seconds buffer after the internet is disconnected after which the entries are removed from the firebase real tim edatabase. Is it possible to configure this time from 60 seconds to lets say 30 seconds? . I basically want the entries to be removed from the presence as soon as the internet is disconnected. If not immediately then at least sooner than a minute alteast.Does anybody have any idea on this?
There are two ways an onDisconnect handler can be triggered on the Firebase servers:
A clean disconnect is when the client has time to inform the server that it is about to disconnect, and in that case the server executes the onDisconnect handlers immediately.
A dirty disconnect is when the client disconnects without informing the server. In this case the server detects that the client is gone when the socket from that client times out, and then it executes the onDisconnect handlers.
Detecting the dirty disconnects takes some times (upwards to a few minutes) and there's no way for you to configure it.
If you want more granular presence detection, the common approach is to periodically write a timestamp into the database from the client. This indicates when the client was last active, and can be used by the other clients to then indicate the liveliness of that user/client.
Related
Two peers are connected - host and client
Client gets offline and iceConnectionState - 'disconnected' on host is triggered after about 3-7 seconds
Why is there a delay ? and how to remove that delay?
I just wanted get online status of user in realtime
Peer connection gets closed when there's no data coming for several seconds, hence the delay. There's no Web API to configure this timeout.
I see three ways how you might reduce the delay:
Send keepalives between peers via datachannel. If there were no keepalive for n milliseconds, client is considered disconnected; connection might be closed (or "pending" user status can be displayed).
Some timeout is still needed, but it can be shorter than the default one.
If client always sends the video, you might use getStats() to detect when there's no new packets coming => client disconnected. It's the same as 1, but using video packets instead of keepalive messages. See this answer.
Send a message to server when client is about to disconnect so the server knows connection is no longer needed and close it. For example, you could send a "goodbye" message to WebRTC datachannel in window.onbeforeunload callback. The drawback is that if client goes offline without closing the page you still have to wait for n seconds, whatever is default in your browser.
I have implemented pubnub to create a socket connection for receiving real-time messages.
There is one thing which I noticed in my developer tools is that - the pubnub heartbeat state shows pending for a particular interval, mostly between 4.3-5 min.
After going through their documents, I realised the timeout can be modified and the default value is 320 seconds. After implementing this feature for my website I can notice some lag, I am not sure if it is pubnub who is causing the issue.
Please let me understand the the idea behind the pending state. Also, if it has an impact on memory. If yes, then how is the impact related to increase or decrease in the heartbeat interval ?
FYI, my pubnub settings only consist of publisher key, subscriber key, uuid and ssl (true)
PubNub Subscribe Connection and Long Poll Cycle
You are seeing the heartbeat query param but that is not the "presence heartbeat" API. That is the subscribe long poll connection which will remain open until:
a message is published on one of the channels you are subscribed to
or, if no messages were published on one of the subscribe channel after 280s, the connection is closed (200 response with no messages) and the SDK will open a new subscribe connection.
PENDING Connection
PENDING just means the subscribe connection is Open and waiting for messages to be published. This is expected.
I highly recommend that you do not change this value unless there is a good reason. Did you make it longer or shorter?
Shorter long poll has little value and practically no harm, technically speaking, but will result in more subscribe/edge transactions.
Longer long poll has an actual technical downside in that your client will disconnect after the 280s expiration but will not reconnect until the end of the new custom expiration time that you set for the client.
The only time you should set the value shorter is if you have an ISP that proactively closes "idle" (pending) connections quicker than 280s. This is very rare but it does happen.
And you will likely see that the subscribe connection gets CANCELED. This happens when the client app changes its channel subscription list: subscribe to a new channel or unsubscribe from an existing channel.
No Impact on Memory
But you are asking if there is some sort of impact on memory. The answer to that is - it should NOT have a negative impact. If you follow Nicolas Fodor's answer/advice, you might be able to confirm that but 1000's of customers into this, we have not had any memory issues with our JavaScript SDK related to this. Just be sure you are using the latest version of our SDKs and report any bugs/issues you find to PubNub Support with full details.
Presence Heartbeat
One more thing about the heartbeat query param value - it typically defaults to 300 (seconds) which is only important when you are using PubNub Presence. If the PubNub server doesn't hear from a client within that 300 second (or whatever it is set to) period, a presence timeout event, on behalf of that client, is sent to anyone listening for presence events. A timeout is like a delayed leave event.
See also:
Connection Managment Docs
Detect and Manage Presence Events
Simple way to find out would be to check performances under load testing before and after the parameter change and without changing any other parameter. If a cause is established you can then vary the parameter value to assess the elasticity of the side effect.
I'm working on node application that would monitor user's online status. It uses socket.io to update online status for users we "observe" (as in, the users we are aware of on the page we're at). What I would like to introduce now is idle status, which would basically mean that after X time of inactivity (as in no request) the status would change from online to idle.
I do monitor all the sockets thus I know when connection was made, so I thought of using this.
My idea is to use setTimeout on every connection for this particular uses (clearing out the previous one if exists) and in setTimeout I would simply change user's status to idle and emit that status change to observers.
What I'm concerned about is performance and scalability of setting and clearning the timeout on every connection. So the question is, are there any issues in terms of the two above with such approach? Is there a better thing of doing it, perhaps a library that is better at handling such things?
I was thinking about extending the functionality of node.js server running Socket.io which I am currently using to allow a client (iOS app) to communicate with a server, so that it could have persistent session data between connection.
On initial connection the server passes the client a session id which the client will store and pass to the server later on if it reconnects after disconnecting, this would allow the client to resume its session without having to re-provide the server with certain information about its current state (obviously when it comes to actual implementation it will be more secure than this).
I want to make it so that the session eventually expires, so it has a max lifetime or if it hasn't been continued after a certain time it times-out. To do this I was thinking of using timers for each session. Im not actually sure how node.js or javascript timers (setTimeout) work in the background and am concerned that having 1000s of session timers could lead to a lot of memory/cpu usage. Could this be a potential issue, should I have a garbage collector that cycles every minute or so and deletes expired session data? What is the kind of most optimal way in terms of least impact on performance method I can do to accomplish this, or are timers already exactly that?
They are used frequently for timeouts, and are very efficient in cpu.
// ten_thousand_timeouts.js
for (var i=0;i<=10000;i++) {
(function(i){
setTimeout(function(){
console.log(i);
},1000)
})(i)
}
With 10,000 the results of logs only took .336 seconds and the act of logging it to the console took most of that time.
//bash script
$> time node ten_thousand_timeouts.js
1
...
9999
10000
real 0m1.336s
user 0m0.275s
sys 0m0.146s
I cannot imagine this being an issue for your use case.
I have browser client Javascript which opens a WebSocket (using socket.io) to request a long-running process start, and then gets a callback when the process is done. When I get the callback, I update the web page to let the user know the process has completed.
This works ok, except on my iPad when I switch to another app and then come back (it never gets the callback, because I guess the app is not online at the time). I'm assuming the same thing will happen on a laptop or other computer that sleeps while waiting for the callback.
Is there a standard way (or any way) to deal with this scenario? Thanks.
For reference, if you want to see the problem page, it is at http://amigen.perfectapi.com/
There are a couple of things to consider in this scenario:
Detect the app going off/on line
See: Online and offline events.
When your app detects the online event after the computer wakes up you can get any information that you've missed.
For older web browsers you'll need to do this in a cleverer way. At Pusher we've added a ping - pong check between the client and server. If the client doesn't receive a ping within a certain amount of time it knows there's a connection problem. If the server sends a ping and doesn't get a pong back with a certain time it knows there's a problem.
A ping pong mechanism is defined in the spec but a way of sending a ping or pong hasn't been defined on the WebSocket API as yet.
Fetching missed information
Most realtime servers only deliver messages to connected to clients. If a client isn't connected, maybe due to temporary network disturbance or their computer has been asleep for a while, then those clients will miss the message.
Some frameworks do provide access to messages through a history/cache. For those that don't you'll need to detect the problem (as above) and then fetch any missed messages. A good way to do this is by providing a timestamp or sequence ID with each messages so you can make a call to your web server to say "give me all messages since X".