-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-authenticating after server disconnect #25
Comments
Hi @Fiveside |
Hi @alaa-eddine, I'm actually working with some code where the server calls methods on the client when they authenticate. The problem also exists with client -> server RPC, but this example is more consise and doesn't rely on server.jsvar http = require('http');
var httpServer = http.createServer();
var Eureca = require('eureca.io');
var eurecaServer = new Eureca.Server({
allow: ["hello"],
authenticate: function (authToken, next) {
console.log('Called Auth with token', authToken);
if (authToken == 'OK') next();
else next('Auth failed');
}
});
eurecaServer.on("connect", function(client) {
client.clientProxy.hello("Hello").then(function(response) {
console.log("Got response back! " + response);
});
});
eurecaServer.attach(httpServer);
httpServer.listen(8000); client.jsvar Eureca = require('eureca.io');
var client = new Eureca.Client({
uri: 'ws://localhost:8000/',
autoConnect: false,
});
client.exports.hello = function(greeting) {
return greeting + " World!";
}
client.on("authResponse", function(result) {
console.log("auth response", result)
})
client.connect()
client.authenticate("OK")
The second time the server starts up, the client doesn't attempt to authenticate with the server. As a result, all RPC calls do not execute properly. |
well this behavior is "normal". when a server is killed, it lose authentication information, when it's back online it's up to you to decide what you want to do because here you have different possible use cases. if you want to handle auto-reconnect, you need to store some information to identify connected client on some persistent storage. I can maybe add some method to the server which will explicitely consider a client as authenticated so that the developer can use it in such scenario. When I'll have some spare time, I'll try to make an example to persist the authentication with a server kill/restart. |
Instead of storing client ids, it seems like it'd be more efficient to just reauthenticate on reconnection. Your comment gave me the idea that we can simply reauthenticate on connection though and this seems to work. client.on("connect", function() {
client.authenticate("OK");
}); On the server side, we would need to add another post-authentication call into the authentication callback function to detect when clients have successfully authenticated. This would work, but it sounds a little messy. Perhaps it might be cleaner to fire an |
I'm preparing the version 0.6.5 with some optimizations and new internal features.
I personnally prefer the first approach, but maybe I'm missing some usecase whre the second option is better ? |
I pushed a new version to github repository (will update the npm package later). this version introduce 'authentication' event. |
Fantastic! I'll try it out tonight! |
I've run into an interesting issue where clients do not re-authenticate after disconnections when you define an authentication callback. Specifically, if you kill the server without killing the client, then RPC calls will not complete saying authentication is still required.
The text was updated successfully, but these errors were encountered: