Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Client connection stream object.

In the case of `net.createServer`, `conn` 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`][websocket-stream], it's the `stream` argument passed to the websocket `handle` function in [`websocket-stream #on-the-server`][websocket-stream-doc-on-the-server]].
In the case of [`ws`][ws], it's the `stream.Duplex` argument passed to the `handle` function.

## client.req

- `<http.IncomingMessage>`

only for [`websocket-stream`][websocket-stream]. It is a HTTP Websocket upgrade request object passed to websocket `handle` function in [`websocket-stream #on-the-server`][websocket-stream-doc-on-the-server]. It gives an option for accessing headers or cookies.
only for [`ws`][ws]. It is a HTTP Websocket upgrade request object passed to the `handle` function. It gives an option for accessing headers or cookies.

## client.connecting

Expand Down Expand Up @@ -152,5 +152,4 @@ Clear all outgoing messages (QoS > 1) related to this client from persistence
[SUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#subscribe
[UNSUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#unsubscribe

[websocket-stream]: https://www.npmjs.com/websocket-stream
[websocket-stream-doc-on-the-server]: https://github.com/maxogden/websocket-stream/blob/master/readme.md#on-the-server
[ws]: https://www.npmjs.com/package/ws
11 changes: 9 additions & 2 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@ server.listen(port, function () {
```js
const aedes = require('aedes')()
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
const ws = require('ws')
const port = 8888

ws.createServer({ server: httpServer }, aedes.handle)
const wss = new ws.WebSocketServer({
server:httpServer
})

wss.on('connection', (websocket, req) => {
const stream = ws.createWebSocketStream(websocket)
aedes.handle(stream, req)
})

httpServer.listen(port, function () {
console.log('websocket server listening on port ', port)
Expand Down
13 changes: 9 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
const aedes = require('./aedes')()
const server = require('net').createServer(aedes.handle)
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
const ws = require('ws')
const port = 1883
const wsPort = 8888

server.listen(port, function () {
console.log('server listening on port', port)
})

ws.createServer({
server: httpServer
}, aedes.handle)
const wss = new ws.WebSocketServer({
server
})

wss.on('connection', (websocket, req) => {
const stream = ws.createWebSocketStream(websocket)
aedes.handle(stream, req)
})

httpServer.listen(wsPort, function () {
console.log('websocket server listening on port', wsPort)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"tap": "^16.3.10",
"tsd": "^0.32.0",
"typescript": "^5.8.3",
"websocket-stream": "^5.5.2"
"ws": "^8.18.2"
},
"dependencies": {
"aedes-packet": "^3.0.0",
Expand Down
13 changes: 9 additions & 4 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require('tap')
const http = require('http')
const ws = require('websocket-stream')
const ws = require('ws')
const mqtt = require('mqtt')
const { setup, connect, delay } = require('./helper')
const aedes = require('../')
Expand Down Expand Up @@ -720,7 +720,7 @@ test('Test queue limit', function (t) {
})
})

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

Expand All @@ -738,9 +738,14 @@ test('websocket clients have access to the request object', function (t) {
})

const server = http.createServer()
ws.createServer({
const wss = new ws.WebSocketServer({
server
}, broker.handle)
})
wss.on('connection', (websocket, req) => {
// websocket is a WebSocket, but aedes expects a stream.
const stream = ws.createWebSocketStream(websocket)
broker.handle(stream, req)
})

server.listen(port, function (err) {
t.error(err, 'no error')
Expand Down