Skip to content

Instantly share code, notes, and snippets.

@imksoo
Last active March 16, 2023 05:40
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imksoo/61e27052f67a5a992b29a3252ea047bb to your computer and use it in GitHub Desktop.
Save imksoo/61e27052f67a5a992b29a3252ea047bb to your computer and use it in GitHub Desktop.
Nostrのリレーから過去ログをあさってくるnode.js
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