Skip to content

Commit

Permalink
implement logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 6, 2019
1 parent 75f7b4d commit 8313039
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ Peers using the same signalling server will find each other. You can specify sev
const provider = new WebrtcProvider('prosemirror', ydoc, { signalling: ['wss://y-webrtc-ckynwnzncc.now.sh', 'ws://localhost:4444'] })
```

### Logging

`y-webrtc` uses the `lib0/logging.js` logging library. By default this library disables logging. You can enable it by specifying the `log` environment / localStorage variable:

```js
// enable logging for all modules
localStorage.log = 'true'
// enable logging only for y-webrtc
localStorage.log = 'y-webrtc'
// by specifying a regex variables
localStorage.log = '^y.*'
```

```sh
# enable y-webrtc logging in nodejs
DEBUG='y-webrtc' node index.js
```

## License
Yjs is licensed under the [MIT License](./LICENSE).

Expand Down
30 changes: 9 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"simple-peer": "^9.6.2",
"lib0": "^0.1.4"
"lib0": "^0.1.5"
},
"devDependencies": {
"rollup": "^1.27.8",
Expand All @@ -50,7 +50,7 @@
"rollup-plugin-terser": "^5.1.2",
"standard": "^12.0.1",
"y-protocols": "^0.1.0",
"lib0": "^0.1.4",
"lib0": "^0.1.5",
"yjs": "13.0.0-102"
},
"peerDependenies": {
Expand Down
16 changes: 14 additions & 2 deletions src/y-webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Peer from 'simple-peer/simplepeer.min.js'
import * as syncProtocol from 'y-protocols/sync.js'
import * as awarenessProtocol from 'y-protocols/awareness.js'

import * as logging from 'lib0/logging.js'

const log = logging.createModuleLogger('y-webrtc')

const messageSync = 0
const messageQueryAwareness = 3
const messageAwareness = 1
Expand Down Expand Up @@ -44,6 +48,7 @@ const checkIsSynced = webrtcRoom => {
if ((!synced && webrtcRoom.synced) || (synced && !webrtcRoom.synced)) {
webrtcRoom.synced = synced
webrtcRoom.provider.emit('synced', [{ synced }])
log('synced ', logging.BOLD, webrtcRoom.name, logging.UNBOLD, ' with all peers')
}
}

Expand Down Expand Up @@ -71,6 +76,7 @@ const readPeerMessage = (peerConn, buf) => {
const syncMessageType = syncProtocol.readSyncMessage(decoder, encoder, doc, webrtcRoom.provider)
if (syncMessageType === syncProtocol.messageYjsSyncStep2 && !webrtcRoom.synced) {
peerConn.syncedRooms.add(roomName)
log('synced ', logging.BOLD, roomName, logging.UNBOLD, ' with ', logging.BOLD, peerConn.remotePeerId)
checkIsSynced(webrtcRoom)
}
if (syncMessageType === syncProtocol.messageYjsSyncStep1) {
Expand Down Expand Up @@ -122,6 +128,7 @@ export class WebrtcConn {
* @param {Array<string>} announcedTopics
*/
constructor (signalingConn, initiator, remotePeerId, announcedTopics) {
log('establishing connection to ', logging.BOLD, remotePeerId)
this.remotePeerId = remotePeerId
this.closed = false
this.connected = false
Expand All @@ -137,6 +144,7 @@ export class WebrtcConn {
signalingConn.send({ type: 'publish', topics: announcedTopics, to: remotePeerId, from: peerId, messageType: 'signal', data })
})
this.peer.on('connect', () => {
log('connected to ', logging.BOLD, remotePeerId)
this.connected = true
announcedTopics.forEach(roomName => {
const room = webrtcRooms.get(roomName)
Expand Down Expand Up @@ -172,8 +180,10 @@ export class WebrtcConn {
checkIsSynced(room)
})
this.peer.destroy()
log('closed connection to ', logging.BOLD, remotePeerId)
})
this.peer.on('error', () => {
this.peer.on('error', err => {
log('error in connection to ', logging.BOLD, remotePeerId, ': ', err)
this.connected = false
this.closed = true
})
Expand Down Expand Up @@ -232,6 +242,8 @@ export class SignallingConn extends ws.WebsocketClient {
}
}
})
this.on('connect', () => log(`connected (${url})`))
this.on('disconnect', () => log(`disconnect (${url})`))
}
/**
* @param {Array<string>} rooms
Expand All @@ -255,7 +267,7 @@ export class WebrtcProvider extends Observable {
* @param {Object} [opts]
* @param {Array<string>} [opts.signalling]
*/
constructor (room, doc, { signalling = ['wss://y-webrtc-hrxsloqrim.now.sh'] } = {}) {
constructor (room, doc, { signalling = ['wss://y-webrtc-hrxsloqrim.now.sh', 'wss://y-webrtc-signalling-eu.herokuapp.com', 'wss://y-webrtc-signalling-us.herokuapp.com/'] } = {}) {
super()
this.room = room
this.doc = doc
Expand Down

0 comments on commit 8313039

Please sign in to comment.