SocketIO - Does a browser refresh cause "on disconnect" to fire? - websocket

I'm using SocketIO and I need to disconnect a socket on the server side if the user presses refresh. I've tried implementing this like so:
io.on('disconnect', function(socket) {
socket.close();
})
But the disconnect event isn't firing. Is this intended behaviour and if so is there any way to disconnect a socket when a page is refreshed?

Yes, when implemented properly a browser refresh causes a disconnect event on the server. I just confirmed this with a simple test app.
The disconnect event is a socket event, not an io event. So, you need to register for it on the socket, not at the io level.
This is my event handler that works just fine on my test server:
io.on('connection', function(socket) {
socket.on('disconnect', function() {
console.log("disconnect: ", socket.id);
});
});
FYI, you do not need to close the socket yourself. That will happen automatically as all webSocket/socket.io connections are automatically closed whenever a page is refreshed or navigated away from.

Related

Is it possible that a Javascript WebSocket could connect before the `open` event listener is added, so you would then miss the event?

This seems to be the canonical way to open a browser-side websocket and then wait for the open event...
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', (event) => {
socket.send('Hello Server!');
});
According to the standard...
socket = new WebSocket(url [, protocols ])
Creates a new WebSocket object, immediately establishing the associated WebSocket connection.
If the new immediately starts establishing the connection, is it possible on a very fast link (like, say, to localhost?) that the connection could be established and the open event fired before the line adding the listener is reached? And then the event would be missed?

Make requests on chromecast receiver shutdown event

I'm developing a chromecast custom receiver, and I need to send a request to the server once the sender disconnects from receiver. When I use ios or android senders, I can see that the 'sender disconnect' event is triggered, but when I use the browser, that event is not triggered.
Because of that, I'm trying to use the 'shutdown' event, to send that request, once the sender is disconnected.
I tried to pass an 'async/await' function as callback to the shutdown event, and tried to force an 'await' on the request, but I get the ' Application should not send requests before the system is ready (they will be ignored)'
I also tried to use the window 'beforeunload' event, but with no success.
Is there any way to send a request, once the 'shutdown' event is triggered?
Cheers
For us the Dispatching Shutdown event occurred in both cases. Web and Mobile senders. So I'm not sure what's happening on your end but will definitely need more information regarding what you're doing in order to look further into it.
Anyhow, if you are setting addEventListeners on your end like this:
context.addEventListener(cast.framework.system.ShutdownEvent,
()=>{
console.log("ShutdownEvent Called");
});
context.addEventListener(cast.framework.system.SenderDisconnectedEvent,
()=>{
console.log("SenderDisconnectedEvent called");
});
Which will result in your calls not firing. You should instead be doing:
context.addEventListener(cast.framework.system.EventType.SHUTDOWN,
()=>{
console.log("ShutdownEvent Called");
});
context.addEventListener(cast.framework.system.EventType.SENDER_DISCONNECTED,
()=>{ console.log("SenderDisconnectedEvent called"); });
Here's the docs for their reference: https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.system#.EventType

Opentok Javascript: On self session/connection disconnect?

Opentok Javascript: On self session/connection disconnect?
I tried all the possible events, following code logs into the console when other deivces disconnects, but it never logs anything when it, itself, disconnects. Please point toward the even which is fired when the session is disconnected. I have tried sessionDisconnect as well without any luck.
this.session.on("connectionDestroyed", function(event) {
console.log(event);
});
Thanks in advance.
TokBox Developer Evangelist here.
The Session object dispatches SessionDisconnectEvent object when a session has disconnected. You mentioned that you tried, sessionDisconnect, but the event is actually sessionDisconnected.
You can listen to the event as such:
this.session.on("sessionDisconnected", function(event) {
console.log(event);
});
The connectionDestroyed event is dispatched when other connections leave the session.

How to configure my sails app with https socket connection?

I have a sails backend API. And I already have an https connection.
(https://myapp.com/api)
How do I connect to a sails socket with my client side (Android and iOS) having an https connection? I dont have any problem connecting to a non HTTPS server. Hope there is a help.
all you have to do is open config/bootstrap.js, and made following changes there
module.exports.bootstrap = function(cb) {
// handle connect socket first event executes after logged in
sails.io.on('connect', function (socket){
socket.emit("connected",{ data: "here am i!" })
});
// handle custom listener for other stuff
sails.io.on('ping', function (socket){
socket.emit("pong",{ data: "send to android/ios/web client" })
});
cb();
};
here you can listen for multiple events as well as you can emit multiple private/broadcast messages as well and also all socket.io listeners will work here

socket.io client events not firing

According to the [docs][1]
[1]: http://socket.io/docs/client-api/, the following events should be firing on the client when connecting, error and disconnecting etc:
connect. Fired upon connecting.
error. Fired upon a connection error
disconnect. Fired upon a disconnection.
reconnect. Fired upon a
successful reconnection.
How do I listen to this socket events on the client? I am using the following but events are not being fired:
var socket = io.connect(); // does connect fine
socket.on('connect', function() {
console.log('connect fired');
});

Resources