moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 507 forks source link

Handling client's keepalive time on server side. #482

Closed mudassir-ngineous closed 8 years ago

mudassir-ngineous commented 8 years ago

Hey Hi! I just wanted to ask is there a way where on server side I can decide what should be the maximum keepalive time of a client and if client requests mosca server with more than the maximum keepalive time what is the best and stable way to overwrite it and set it to the maximum value.

I tried doing following and I also succeeded, but just wanted to assure its stability...

Client object: var client_id = "testClient"; var client = mqtt.connect({ port: 1883, host: 'myserver.com', keepalive: 600,clientId:client_id});

Server side: When "clientConnected" event gets triggered, the callback has client object. which has client.timer, an instance of Retimer which has "reschedule" prototype but "reschedule" checks whether the new timeout is greater than old timeout then only it reschedules otherwise it keeps the client's requested timeout period.

But in my application I want to overwrite the _scheduled value if it just crosses some max-limit. so I did it as below:

server.on("clientConnected", function(client) { if(client.timer._scheduled > 2_60_1000){ // client.timer.reschedule(2_60_1000); // it was returning false i.e. was not rescheduling client.timer._started = Date.now(); // directly overwritten client.timer._scheduled = 2_60_1000; // directly overwritten } });

As I know _started & _scheduled are members of Retimer and the names of variables may get changed in further next of mosca, how stable the solution could be? Or is their an alternate way to accomplish the same?

mcollina commented 8 years ago

@mudassir-ngineous the problem is that keepalive is an agreement between the client and the server. The server has no way to communicate the shortened keep alive to the client. I would highly recommend you to avoid doing that.

If you do not have control of your clients and they ask for a longer keepalive that you can support, then do not allow them to connect. You can access client.keepalive inside the authenticate function.