Replying to Avatar Anthony Accioly

Morning folks. Quick one, I'm working (well, actually, talking my mouth off and getting in the way of their good work 🤣) with Keyoxide folks on their Matrix channel. They are implementing Nostr support and having an interesting issue with nostr-tools.

Apparently

```

import { WebSocket } from 'ws'

useWebSocketImplementation(WebSocket)

```

Is not setting the WS in their environment. Bizarrely, manually setting the variable works.

```

pool._WebSocket = WebSocket

```

See: https://code.jaenis.ch/gists/nostr-tools-websocket-closure-leak/src/branch/main/nostr.js#L28

If you comment line 28 things break.

The code is runnable (`npm i && npm run test`).

I'm a bit clueless when it comes to TypeScript, but it feels like this was meant be somewhat of a "private" global variable. Somehow `useWebSocketImplementation` is not working in this example. After calling `useWebSocketImplementation` WebSocket is still undefined.

Anyone more familiar with TypeScript and Nostr tools can have a look?

#devstr #asknostr #nostrtools #typescript #websockets

nostr:nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gprfmhxue69uhkcmmrdd3x77pwve5kzar2v9nzucm0d5hsh2c3z4, nostr:nprofile1qqsgzfdez8ksa9xmuvqg5zly3nl9e5xqkpvj8nllj9aw06ra4pqq3qcpz4mhxue69uhhyetvv9ujuerpd46hxtnfduhszrnhwden5te0dehhxtnvdakz7qgswaehxw309ahx7um5wghx6mmd9uqdl3vp, nostr:nprofile1qqsy40y0zl3suj0gh9sw4qs3elxxgjxvjz9hg8nlnqvlqehjwfrssjqpz4mhxue69uhkummnw3ezummcw3ezuer9wchszrnhwden5te0dehhxtnvdakz7qg4waehxw309aex2mrp0yhxgctdw4eju6t09u979cgl (just tagging random Nostr Tools commiters that are well versed in TS / JS), sorry for spamming.

Reply to this note

Please Login to reply.

Discussion

nostr:npub1a6we08n7zsv2na689whc9hykpq4q6sj3kaauk9c2dm8vj0adlajq7w0tyc you are using the relay function, you need to use the pool version, this worked fine for me:

```

import assert from 'node:assert/strict'

import { useWebSocketImplementation } from 'nostr-tools/pool'

import WebSocket from 'ws'

useWebSocketImplementation(WebSocket)

import { SimplePool, nip19, verifiedSymbol } from 'nostr-tools'

```

Maybe you will need to install the ws module and add `"type": "module"` to your package.json.

Ah, wrong import error. Classic :). Thank you very much!

Thank you too 👍