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

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?

Related

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

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

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.

Websocket closing issue

I am working on web application for client side which is user websocket connection to send/receive data.
Here is my problem.
If some parameter change on client side, I want to close my web socket connection and initiate new connection with new paramaeters.
I tried to close web socket connection bu using close(). But when I checked my browser console, websocket status is always 2.
Please help me to resolve this.
I am not completely sure of what problem you have, but if you set a handler for onclose, it will be triggered when you call close.
var ws = new WebSocket('wss://wss://echo.websocket.org');
ws.onclose=function(){
alert('The websocket just closed, readyState: ' + ws.readyState);
}
Demo: http://jsfiddle.net/6kh20bdy/
ReadyState 3 means closed: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Constants

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');
});

Dart, how to listen for websocket close on server side?

I see that client side websockets have an onlClose stream which is useful, but why isn't there a complimentary onclose stream on the server side websocket? I would like to be able to clean up my websessions on the server side when the web socket closes but Im not sure how to detect that event occuring on the server side.
The server-side WebSocket API is a Stream. That means that to listen on data, you can do the following:
websocket.listen((data) {
// Do something with data.
}, onDone: () {
// No more data - read-direction was closed.
});
Where the onDone callback is invoked, when the WebSocket is closed.

Resources