How to configure my sails app with https socket connection? - socket.io

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

Related

How to create multiple XMPP clients in nodejs using websockets

I have an electron app that creates a websocket connection to a node js server. It sends a JSON request to that server telling it to create a xmpp client.
let message = {
action: "setupXmpp",
data: {
username,
password,
},
};
socket.send(JSON.stringify(message));
Within that server I have a switch that reads the message action and creates the xmpp client. The code in xmppActions is standard boilerplate taken from xmpp's repo
const xmppActions = require("./Webapp/xmppActions");
case "setupXmpp":
console.log(`Received setupXmpp request`);
var { username, password } = message.data;
const xmpp = xmppActions.setUpXMPPconn(username, password);
xmpp.on("online", async (address) => {
console.log("▶", "online as", address.toString());
ws.send("Register xmpp Success!");
});
break;
Everything works fine I can create an xmpp client and send messages, all good.
My issue is when i have two clients open and they both register (with different username and password ) whoever is the last request always overrides the previous register. I've done a wireshark trace and the two websocket connections are created as I would expect but when it comes to sending messages they both use the most recent register. I assume it's because the XMPP client is a constant and whoever is last it uses those for all future requests.
How do I make it so that each websocket connection gets its own XMPP client almost like a request scoped client specific for each websocket.
I had a constant outside the websocket connection, changed it to have a var inside so each connection had it own client.

Connect Laravel echo from Angular project

I have a Laravel-echo-server with Redis running on my local.
I created a test API endpoint, that emits broadcastable event.
on http://localhost:8000/api/web-socket-test I see the response in echo server CLI.
I set-up laravel-echo auth key and I can get the stat info from server API
http://localhost:6001/apps/APP_ID/status?auth_key=b73a61d0.
The problem is with connecting to echo-server from Angular via ws: protocol.
My connection code is
import {webSocket, WebSocketSubject} from 'rxjs/webSocket';
export class MyComponent implements OnInit, OnDestroy {
myWebSocket: WebSocketSubject<any> = webSocket('ws://127.0.0.1:6001');
ngOnInit() {
this.myWebSocket.subscribe(
msg => console.log('message received: ' + msg),
err => console.log(err),
() => console.log('complete')
);
}
And finally I've got an error: WebSocket connection to 'ws://127.0.0.1:6001/' failed: Connection closed before receiving a handshake response.
How can I establish ws connection?
I believe you want to try connecting using the socket.io client libraries instead of using rxjs raw websockets.
Although it's not immediately clear from the laravel echo server docs, the project title states it's a 'Socket.io server for Laravel Echo'. So I'm assuming you should use the socket.io client libraries for connections.

socket.io to talk to server that is implementing RFC6455 in a strict fashion

I am trying to make my socket.io javascript client talk to a server implemented in cpp using websocketpp and its not working. Its surprising that I cant configure socket.io to fall back to real websockets when I need them.
Any one has any ideas or suggestions on this ? going back to websocket npm and re implementing my client is the only way ?
I tried this, but it does not work
var socket = io.connect('http://localhost:8080', {
transports: [
'websocket',
'polling'
]
});
socket.io is an additional protocol on top of webSocket so a socket.io client can ONLY talk to a socket.io server. While socket.io uses webSocket for the transport, it needs support for it's additional layer on top of webSocket to work properly.
If you want to talk to a plain webSocket server, then you should use a plain webSocket client.
var socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
socket.send('Hello Server!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server', event.data);
});
You can probably find a socket.io server modules for cpp if you'd like to fix the server-side of things to talk socket.io.

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.

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