Skip to content

Commit ead0f4a

Browse files
committed
Merge pull request mqttjs#334 from kokeksibir/master
Session Present flag is needed while handling connect event
2 parents 627521d + 418b2e7 commit ead0f4a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,12 @@ version 1.3 and 1.4 works fine without those.
212212

213213
#### Event `'connect'`
214214

215-
`function() {}`
215+
`function(connack) {}`
216216

217-
Emitted on successful (re)connection (i.e. connack rc=0).
217+
Emitted on successful (re)connection (i.e. connack rc=0).
218+
* `connack` received connack packet. When `clean` connection option is `false` and server has a previous session
219+
for `clientId` connection option, then `connack.sessionPresent` flag is `true`. When that is the case,
220+
you may rely on stored session and prefer not to send subscribe commands for the client.
218221

219222
#### Event `'reconnect'`
220223

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ MqttClient.prototype._handleConnack = function (packet) {
667667
clearTimeout(this.connackTimer);
668668

669669
if (0 === rc) {
670-
this.emit('connect');
670+
this.emit('connect', packet);
671671
} else if (0 < rc) {
672672
this.emit('error',
673673
new Error('Connection refused: ' + errors[rc]));

test/abstract_client.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ module.exports = function (server, config) {
199199
client.once('error', done);
200200
});
201201

202+
it('should provide connack packet with connect event', function (done) {
203+
server.once('client', function (serverClient) {
204+
serverClient.connack({returnCode: 0, sessionPresent: true});
205+
206+
server.once('client', function (serverClient) {
207+
serverClient.connack({returnCode: 0, sessionPresent: false});
208+
});
209+
});
210+
211+
var client = connect();
212+
client.once('connect', function (packet) {
213+
should(packet.sessionPresent).be.equal(true);
214+
client.once('connect', function (packet) {
215+
should(packet.sessionPresent).be.equal(false);
216+
client.end();
217+
done();
218+
});
219+
});
220+
});
221+
202222
it('should mark the client as connected', function (done) {
203223
var client = connect();
204224
client.once('connect', function () {

0 commit comments

Comments
 (0)