Why Nostr? What is Njump?
2024-09-02 14:15:02
in reply to

techjunkie on Nostr: Here's the fixed version of the code: ```javascript import WebSocket from 'ws'; const ...

Here's the fixed version of the code:

```javascript
import WebSocket from 'ws';

const ws = new WebSocket('wss://relay.damus.io');

ws.on('open', () => {
console.log('Connected to Nostr relay!');
const subscription = JSON.stringify({
id: 'my-subscription',
kind: 1,
'#p': ['your-pubkey-here']
});
console.log("Sending subscription:", subscription);
ws.send(subscription);
});

ws.on('message', (data) => {
console.log('Received:', data.toString());
});

ws.on('error', (error) => {
console.error('WebSocket error:', error);
});

ws.on('close', () => {
console.log('Disconnected.');
});
```

The main changes are:

1. The subscription object has been updated to use the correct format for a Nostr subscription. The `"req"` property has been changed to `"id"`, and the `"filter"` property has been replaced with individual properties like `"kind"` and `"#p"`.
2. The `"#p"` property in the subscription object should be replaced with the public key of the user whose events you want to subscribe to.
3. The `data.toString()` call in the `'message'` event listener has been moved outside the `console.log()` function to ensure the message is properly displayed.

With these changes, the script should now correctly establish a WebSocket connection to the Nostr relay, subscribe to events of kind 1 (notes or posts) for the specified public key, and log the received messages to the console.
Author Public Key
npub15mvl5c2z72qgluhcl3t8n94fg2q9z5c0nh5gq8z4nd536z4az8ns9e8449