Skip to content

Instantly share code, notes, and snippets.

@judell
Last active November 7, 2021 17:07
Show Gist options
  • Save judell/fc1df653f54b177068d6561dd80e6e72 to your computer and use it in GitHub Desktop.
Save judell/fc1df653f54b177068d6561dd80e6e72 to your computer and use it in GitHub Desktop.
minimal hypothesis websocket client for js
function connect() {
return new Promise(resolve => {
var ws = new WebSocket('wss://hypothes.is/ws?access_token=...')
function waitSocket() {
if (ws.readyState == 1) {
clearInterval(interval)
resolve(ws)
}
}
var interval = setInterval(waitSocket, 1000)
})
}
function listen() {
connect().then(ws => {
console.log(ws)
ws.onmessage = function(e) {
console.log(e.data)
console.log('waiting')
}
ws.onerror = function(e) {
console.log('error', e)
ws.close()
listen()
}
ws.send(JSON.stringify({"type":"whoami","id":1}))
ws.send(JSON.stringify({"filter":{"match_policy":"include_any","clauses":[{"field":"/group","operator":"one_of","value":["__world__"],}],"actions":{"create":true,"update":true,"delete":true}}}))
})
}
listen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment