useWebSocketImplementation() on relay.ts

This commit is contained in:
fiatjaf 2024-02-14 13:19:48 -03:00
parent 7b0458db72
commit 01f13292bb
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 15 additions and 4 deletions

View File

@ -92,10 +92,11 @@ await relay.publish(signedEvent)
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
import 'websocket-polyfill'
import { useWebSocketImplementation } from 'nostr-tools/relay'
useWebSocketImplementation(require('ws'))
```
### Interacting with multiple relays

View File

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

View File

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