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

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 👍

`pool.ts` where `_WebSocket` and `useWebSocketImplementation` can be found:

https://github.com/nbd-wtf/nostr-tools/blob/master/pool.ts