Last active
September 5, 2024 14:52
-
-
Save imksoo/61e27052f67a5a992b29a3252ea047bb to your computer and use it in GitHub Desktop.
Nostrのリレーから過去ログをあさってくるnode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const relay = 'wss://relay-jp.nostr.wirednet.jp' | |
const authors = ['634bd19e5c87db216555c814bf88e66ace175805291a6be90b15ac3b2247da9b'] | |
function history (until = Math.floor(new Date() / 1000)) { | |
let socket | |
if (typeof (WebSocket) === 'undefined') { | |
const WebSocket = require('websocket').w3cwebsocket | |
socket = new WebSocket(relay) | |
} else { | |
socket = new WebSocket(relay) | |
} | |
socket.onmessage = message => { | |
const event = JSON.parse(message.data) | |
if (event[0] === 'EOSE') { | |
socket.send(JSON.stringify(['CLOSE', event[1]])) | |
} else if (event.length < 3) { | |
socket.close() | |
} else { | |
const e = event[2] | |
console.log(e) | |
socket.send(JSON.stringify(['REQ', crypto.randomUUID(), { | |
authors, | |
until: e.created_at - 1, | |
limit: 1 | |
}])) | |
} | |
} | |
socket.onopen = () => { | |
socket.send(JSON.stringify(['REQ', crypto.randomUUID(), { | |
authors, | |
until, | |
limit: 1 | |
}])) | |
} | |
socket.onclose = () => { | |
console.log('Websocket is closed.') | |
} | |
socket.onerror = (e) => { | |
console.log('Websocket encountered error: ', e.message) | |
} | |
} | |
history() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment