useWebSocketImplementation() on relay.ts

This commit is contained in:
fiatjaf
2024-02-14 13:19:48 -03:00
parent 7b0458db72
commit 01f13292bb
3 changed files with 15 additions and 4 deletions

View File

@@ -92,10 +92,11 @@ await relay.publish(signedEvent)
relay.close() relay.close()
``` ```
To use this on Node.js you first must install `websocket-polyfill` and import it: To use this on Node.js you first must install `ws` and call something like this:
```js ```js
import 'websocket-polyfill' import { useWebSocketImplementation } from 'nostr-tools/relay'
useWebSocketImplementation(require('ws'))
``` ```
### Interacting with multiple relays ### Interacting with multiple relays

View File

@@ -7,6 +7,16 @@ import { Queue, normalizeURL } from './utils.ts'
import { makeAuthEvent } from './nip42.ts' import { makeAuthEvent } from './nip42.ts'
import { yieldThread } from './helpers.ts' import { yieldThread } from './helpers.ts'
var _WebSocket: typeof WebSocket
try {
_WebSocket = WebSocket
} catch {}
export function useWebSocketImplementation(websocketImplementation: any) {
_WebSocket = websocketImplementation
}
export class AbstractRelay { export class AbstractRelay {
public readonly url: string public readonly url: string
private _connected: boolean = false private _connected: boolean = false
@@ -74,7 +84,7 @@ export class AbstractRelay {
}, this.connectionTimeout) }, this.connectionTimeout)
try { try {
this.ws = new WebSocket(this.url) this.ws = new _WebSocket(this.url)
} catch (err) { } catch (err) {
reject(err) reject(err)
return return

View File

@@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "nostr-tools", "name": "nostr-tools",
"version": "2.1.8", "version": "2.1.9",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",