-
Notifications
You must be signed in to change notification settings - Fork 26
docs: add custom events howto #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Custom Nostr Events | ||
|
|
||
| This guide shows how to construct and publish a Nostr event with a non-standard `kind` using **nostr-java**. | ||
|
|
||
| ## Background | ||
|
|
||
| Every Nostr event must include the fields defined in [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md): | ||
|
|
||
| - `id` | ||
| - `pubkey` | ||
| - `created_at` | ||
| - `kind` | ||
| - `tags` | ||
| - `content` | ||
| - `sig` | ||
|
|
||
| Kinds that are not defined by existing NIPs may still be used. [NIP-16](https://github.com/nostr-protocol/nips/blob/master/16.md) describes how kind numbers are grouped (regular, replaceable, ephemeral and parameterized replaceable). Choose a value that does not collide with other applications. | ||
|
|
||
| ## Example | ||
|
|
||
| ```java | ||
| import java.util.List; | ||
|
|
||
| import nostr.client.springwebsocket.StandardWebSocketClient; | ||
| import nostr.event.BaseTag; | ||
| import nostr.event.impl.GenericEvent; | ||
| import nostr.event.message.EventMessage; | ||
| import nostr.id.Identity; | ||
|
|
||
| public class CustomEventExample { | ||
| public static void main(String[] args) throws Exception { | ||
| Identity identity = Identity.generateRandomIdentity(); | ||
|
|
||
| int CUSTOM_KIND = 9000; // Non-standard kind | ||
| GenericEvent event = new GenericEvent(identity.getPublicKey(), CUSTOM_KIND, List.<BaseTag>of(), | ||
| "Hello from a custom kind!"); | ||
|
|
||
| // Required fields `id` and `sig` are populated when signing | ||
| identity.sign(event); | ||
|
|
||
| try (StandardWebSocketClient client = new StandardWebSocketClient("wss://relay.example.com")) { | ||
| client.send(new EventMessage(event)); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Steps Explained | ||
|
|
||
| 1. **Construct the event** – Provide the public key, custom kind, any tags, and content. The constructor fills in `created_at` automatically and initializes the list of `tags`. | ||
| 2. **Sign** – `Identity.sign(event)` computes the event `id` and `sig` using the private key. Relays verify these fields against the serialized event bytes as defined in NIP‑01. | ||
| 3. **Submit to a relay** – Send the event using an `EVENT` message. The example uses `StandardWebSocketClient`, but any Nostr-compatible relay transport will work. | ||
|
|
||
| For more information about event structure and relay communication, consult [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md) and [NIP-16](https://github.com/nostr-protocol/nips/blob/master/16.md). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example uses a placeholder relay URL 'wss://relay.example.com' which is not a real relay. Consider using a well-known public relay like 'wss://relay.damus.io' or add a note that users should replace this with an actual relay URL.