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');
});
Related
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?
I'm trying to make my SAPUI5 app deployed on Sap Cloud Platform listen to server-sent events sent from a simple test server.
I found this tutorial: https://auth0.com/blog/developing-real-time-web-applications-with-server-sent-events/
The tutorial explains how to create a React app that displays a table with some information and how to use server-sent events to make the server update the information on the client without the client having to make periodic requests to the server.
I tested it and got it working, and now I'm trying to substitute the React client app for my SAPUI5 app.
I added a HANA destination in my sap account and added the destination in my neo-app file.
This is basically the controller for the app:
onInit: function () {
this.eventSource = new EventSource('/flightinfo/events');
this.eventSource.addEventListener('flightStateUpdate', this.updateFlightState, false);
}
updateFlightState: function(event) {
console.log("Hello");
}
And this is how the destination is declared in the neo-app.json file:
{
"path": "/flightinfo",
"target": {
"type": "destination",
"name": "flightinfo"
},
"description": "flightinfo"
}
I expected my app to print to the console every time the server sent an update, since I'm just testing the connection and not using the data received. However, the app doesn't fire the event that would trigger the console output.
My server log shows the request is received and the response is 200 OK, but the webapp shows the response as timed out:
HTTP Status 504 - Socket connection timed out for host [my server]. Reason: Read timed out (local port [port number] to address [ip address] ([...].od.sap.biz), remote port [port number] to address [ip address] ([...].od.sap.biz))
I redacted IPs and Ports from the error message.
Trying to open the request url from the network tab at chrome responds with:
HTTP Status 503 - No application is available to handle this request
However, navigating manually to the address of my server (the address I introduced in the Destinations tab on SCP) does show me the data that my server is sending, updates and all.
What am I getting wrong? Is there some security policy preventing my server from sending server-sent events through a SCP Destination?
Any help would be greatly appreciated, since I can't seem to find any information about using server-sent events on SAPUI5 anywhere.
Thank you!
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
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.
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