Skip to content

Commit

Permalink
Add websocket/http request to client object during connection (#322)
Browse files Browse the repository at this point in the history
* add websocket/http request to client object during connection

* add README notes and websocket request with headers test

* note that client.req is only present on websocket connections
  • Loading branch information
abachman authored and gnought committed Sep 14, 2019
1 parent 56aa327 commit 18aeb22
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ server.listen(8883, function () {
* <a href="#client"><code><b>Client</b></code></a>
* <a href="#clientid"><code>client.<b>id</b></code></a>
* <a href="#clientclean"><code>client.<b>clean</b></code></a>
* <a href="#clientconn"><code>client.<b>conn</b></code></a>
* <a href="#clientreq"><code>client.<b>req</b></code></a>
* <a href="#clientpublish"><code>client.<b>publish()</b></code></a>
* <a href="#clientsubscribe"><code>client.<b>subscribe()</b></code></a>
* <a href="#clientunsubscribe"><code>client.<b>unsubscribe()</b></code></a>
Expand Down Expand Up @@ -385,6 +387,24 @@ The id of the client, as specified by the CONNECT packet, defaults to 'aedes_' +
`true` if the client connected (CONNECT) with `clean: true`, `false`
otherwise. Check the MQTT spec for what this means.
-------------------------------------------------------
<a name="clientconn"></a>
### client#conn
The client's connection stream object.
In the case of `net.createServer` brokers, it's the connection passed to the `connectionlistener` function by node's [net.createServer](https://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) API.
In the case of [websocket-stream](https://www.npmjs.com/package/websocket-stream) brokers, it's the `stream` argument passed to the `handle` function described in [that library's documentation](https://github.com/maxogden/websocket-stream/blob/e2a51644bb35132d7aa477ae1a27ff083fedbf08/readme.md#on-the-server).
-------------------------------------------------------
<a name="clientreq"></a>
### client#req
The HTTP Websocket upgrade request object passed to websocket broker's `handle` function by the [`websocket-stream` library](https://github.com/maxogden/websocket-stream/blob/e2a51644bb35132d7aa477ae1a27ff083fedbf08/readme.md#on-the-server).
If your clients are connecting to aedes via websocket and you need access to headers or cookies, you can get them here. **NOTE:** this property is only present for websocket connections.
-------------------------------------------------------
<a name="clientpublish"></a>
### client#publish(message, [callback])
Expand Down
4 changes: 2 additions & 2 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ function Aedes (opts) {
this.counter = 0
this.connectTimeout = opts.connectTimeout
this.mq = opts.mq || mqemitter(opts)
this.handle = function handle (conn) {
this.handle = function handle (conn, req) {
conn.setMaxListeners(opts.concurrency * 2)
// create a new Client instance for a new connection
// return, just to please standard
return new Client(that, conn)
return new Client(that, conn, req)
}
this.persistence = opts.persistence || memory()
this.persistence.broker = this
Expand Down
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var handle = require('./handlers')

module.exports = Client

function Client (broker, conn) {
function Client (broker, conn, req) {
var that = this

this.broker = broker
this.conn = conn
this.req = req
this.parser = mqtt.parser()
this.connected = false
this.connackSent = false
Expand Down
48 changes: 48 additions & 0 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ var helper = require('./helper')
var aedes = require('../')
var setup = helper.setup
var connect = helper.connect
var http = require('http')
var ws = require('websocket-stream')
var mqtt = require('mqtt')

;[{ ver: 3, id: 'MQIsdp' }, { ver: 4, id: 'MQTT' }].forEach(function (ele) {
test('connect and connack (minimal)', function (t) {
Expand Down Expand Up @@ -361,3 +364,48 @@ test('reject clients with wrong protocol name', function (t) {
broker.on('closed', t.end.bind(t))
})
})

// websocket-stream based connections
test('websocket clients have access to the request object', function (t) {
t.plan(3)

var broker = aedes()
var server = http.createServer()
ws.createServer({
server: server
}, broker.handle)

server.listen(4883, function (err) {
t.error(err, 'no error')
})

broker.on('client', function (client) {
if (client.req) {
t.pass('client request object present')
if (client.req.headers) {
t.equal('sample', client.req.headers['x-test-protocol'])
finish()
}
} else {
t.fail('no request object present')
}
})

var client = mqtt.connect('ws://localhost:4883', {
wsOptions: {
headers: {
'X-Test-Protocol': 'sample'
}
}
})

var timer = setTimeout(finish, 1000)

function finish () {
clearTimeout(timer)
broker.close()
server.close()
client.end()
t.end()
}
})

0 comments on commit 18aeb22

Please sign in to comment.