Skip to content

Commit

Permalink
book: add client messages JavaScript examples
Browse files Browse the repository at this point in the history
- Updated index.js
- Added client.js with example code
- Updated  05_01-client-message.md to include examples in book
  • Loading branch information
RydalWater committed Nov 8, 2024
1 parent cc0147c commit 88ac19c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
4 changes: 3 additions & 1 deletion book/snippets/nostr/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const keys = require("./src/keys");
const eventJson = require("./src/event/json");
const eventBuilder = require("./src/event/builder");
const filters = require("./src/messages/filters");
const clientMessages = require("./src/messages/client");
const relayMessages = require("./src/messages/relay");
const nip01 = require("./src/nip01");
const nip05 = require("./src/nip05");
Expand All @@ -21,8 +22,9 @@ async function main() {
eventJson.eventJson();
eventBuilder.eventBuilder();

filters.run();
await clientMessages.run();
await relayMessages.run();
await filters.run();

nip01.run();
await nip05.run();
Expand Down
63 changes: 63 additions & 0 deletions book/snippets/nostr/js/src/messages/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { ClientMessage, EventBuilder, Filter, Keys, loadWasmAsync } = require('@rust-nostr/nostr');

async function run() {
await loadWasmAsync();

const keys = Keys.generate();
const event = EventBuilder.textNote("TestTextNoTe", []).toEvent(keys);

console.log()
console.log("Client Messages:");

// ANCHOR: event-message
// Create Event client message
console.log(" Event Client Message:");
let clientMessage = ClientMessage.event(event);
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: event-message

console.log();
// ANCHOR: req-message
// Create Request client message
console.log(" Request Client Message:");
let f = new Filter().id(event.id);
clientMessage = ClientMessage.req("ABC123", [f]);
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: req-message

console.log();
// ANCHOR: close-message
// Create Close client message
console.log(" Close Client Message:");
clientMessage = ClientMessage.close("ABC123");
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: close-message

console.log();
// ANCHOR: parse-message
// Parse Messages from JSON
console.log(" Parse Client Messages:");
clientMessage = ClientMessage.fromJson('["REQ","ABC123",{"#p":["421a4dd67be773903f805bcb7975b4d3377893e0e09d7563b8972ee41031f551"]}]');
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: parse-message

console.log();
// ANCHOR: auth-message
// Create Auth client message (NIP42)
console.log(" Auth Client Message:");
clientMessage = ClientMessage.auth(event);
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: auth-message

console.log();
// ANCHOR: count-message
// Create Count client message (NIP45)
console.log(" Count Client Message:");
f = new Filter().pubkey(keys.publicKey);
clientMessage = ClientMessage.count("ABC123", [f]);
console.log(` - JSON: ${clientMessage.asJson()}`);
// ANCHOR_END: count-message

}

module.exports.run = run;
49 changes: 44 additions & 5 deletions book/src/nostr/05_01-client-message.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,31 @@ When presented with a client message object as either a JSON or an instance of t
<div slot="title">JavaScript</div>
<section>

TODO
The `ClientMessage` class easily handles the construction of the 3 main message types `EVENT`, `REQ`, and `CLOSE`.
In the examples below we can utilize the relevant class methods `event()`, `req()` and `close()`, respectively, to create the client message objects.

Once we have the `ClientMessage` objects we can use the `asJson()` method to present their content.


```javascript,ignore
{{#include ../../snippets/nostr/js/src/messages/client.js:event-message}}
```

Note that when constructing a `REQ` we want to pass through a `Filter` object which will allow the relay to return data meeting a given set of criteria.
Please jump to the [Filter](05_01_01-filter.md) section for more details on how to construct these objects.

```javascript,ignore
{{#include ../../snippets/nostr/js/src/messages/client.js:req-message}}
```

```javascript,ignore
{{#include ../../snippets/nostr/js/src/messages/client.js:close-message}}
```

When presented with a client message object as either a JSON using the `fromJson()` method.

```javascript,ignore
{{#include ../../snippets/nostr/js/src/messages/client.js:parse-message}}
</section>
Expand Down Expand Up @@ -112,7 +136,21 @@ Note that `COUNT` is effectively a specific type of `REQ` message therefore it u
<div slot="title">JavaScript</div>
<section>

TODO
As an extension of the client messaging section of the protocol [NIP-42](https://github.com/nostr-protocol/nips/blob/master/42.md) and [NIP-45](https://github.com/nostr-protocol/nips/blob/master/45.md) introduce two new messaging types `AUTH` and `COUNT`.

The `AUTH` type is designed to facilitate a method by which clients can authenticate with a given relay.
Whereas the `COUNT` type offers a method for clients can request simple counts of events from relays.
These are constructed in much the same way as the earlier message examples, by using the `ClientMessage` class in conjunction with the relevant methods `auth()` and `count()`.

```python,ignore
{{#include ../../snippets/nostr/python/src/messages/client.py:auth-message}}
```

Note that `COUNT` is effectively a specific type of `REQ` message therefore it utilizes the `Filter` object in constructing the criteria which should be used by the relay to return the count value.

```python,ignore
{{#include ../../snippets/nostr/python/src/messages/client.py:count-message}}
```

</section>

Expand All @@ -132,7 +170,7 @@ TODO

</custom-tabs>

## Error Messages
## Negentropy Messages

<custom-tabs category="lang">

Expand All @@ -147,7 +185,8 @@ TODO
<section>

Finally, the `ClientMessageEnum` class also opens up three additional message types `NEG_OPEN()`, `NEG_CLOSE()` and `NEG_MSG()`.
These do not form part of the standard protocol specification but do have specific uses when it comes to providing methods by which error messaging can be handled by clients.
These do not form part of the standard protocol specification but instead form part of an additional protocol [Negentropy](https://github.com/hoytech/negentropy) for handling set-reconciliation.

To construct these we need to first create them as instance of the `ClientMessageEnum` class and then pass these into a `ClientMessage` object using the `from_enum()` method.

```python,ignore
Expand All @@ -167,7 +206,7 @@ To construct these we need to first create them as instance of the `ClientMessag
<div slot="title">JavaScript</div>
<section>

TODO
Not currently available in the Javascript Bindings.

</section>

Expand Down

0 comments on commit 88ac19c

Please sign in to comment.