Get a list of global posts #180
Replies: 7 comments
-
I'm away on vacation this week. I'll look as soon as I can.
--
Sent with Tuta; enjoy secure & ad-free emails:
https://tuta.com
15 Oct 2024, 19:53 by ***@***.***:
…
Hello,
I would like to get a list of the global posts being published on a few selected servers. So far I was able to write a demo program but the text is being written to console. What is needed is to output the events through a callback event.
Question:> How can this code be changed so that one method is called when a new text is published?
I've looked for an answer on the examples, but I'm not yet finding how to get the new texts. I guess maybe an example for that would help other developers too.
Many thanks in advance.
package> > online> .> nostrium> .> apps> .> nostr> ;> import> > lombok> .> extern> .> java> .> Log> ;> import> > nostr> .> api> .> NIP01> ;> import> > nostr> .> base> .> PublicKey> ;> import> > nostr> .> event> .> impl> .> Filters> ;> import> > nostr> .> event> .> Kind> ;> import> > nostr> .> id> .> Identity> ;> import> > nostr> .> util> .> NostrException> ;> import> > java> .> util> .> ArrayList> ;> import> > java> .> util> .> Calendar> ;> import> > java> .> util> .> List> ;> import> > java> .> util> .> Map> ;> import> > java> .> util> .> concurrent> .> ExecutorService> ;> import> > java> .> util> .> concurrent> .> Executors> ;> import> > java> .> util> .> concurrent> .> TimeUnit> ;> import> > java> .> util> .> logging> .> Level> ;> import> > nostr> .> api> .> EventNostr> ;> import> > nostr> .> api> .> Nostr> ;> @> Log> public> > class> > NostrGlobalPostsExample> { > private> > static> > final> > Identity> > SENDER> = > Identity> .> generateRandomIdentity> (); > private> > final> > static> > Map> <> String> , > String> > > RELAYS> = > Map> .> of> ( > "damus"> , > "wss://relay.damus.io"> , > "wellorder"> , > "wss://nostr-pub.wellorder.net"> , > "snort"> , > "wss://relay.snort.social"> ); > public> > static> > void> > main> (> String> [] > args> ) > throws> > Exception> { > try> { > log> .> log> (> Level> .> FINE> , > "================= Start Fetching Global Posts"> ); > ExecutorService> > executor> = > Executors> .> newFixedThreadPool> (> 10> ); > // Submit task to fetch global posts> > executor> .> submit> (() -> { > try> { > filters> (); } > catch> (> InterruptedException> > t> ) { > log> .> log> (> Level> .> SEVERE> , > t> .> getMessage> (), > t> ); } }); > stop> (> executor> ); > if> (> executor> .> isTerminated> ()) { > log> .> log> (> Level> .> FINE> , > "================== The End"> ); } } > catch> (> IllegalArgumentException> > ex> ) { > log> .> log> (> Level> .> SEVERE> , > null> , > ex> ); > throw> > new> > NostrException> (> ex> ); } } > /**> * Fetches global posts (text notes) from relays using filters.> */> > public> > static> > void> > filters> () > throws> > InterruptedException> { > //logHeader("filters");> > // Define the kinds of events you want to fetch (Kind 1: text notes)> > var> > kinds> = > List> .> of> (> Kind> .> TEXT_NOTE> ); > // Time filter: Get posts from now> > var> > date> = > Calendar> .> getInstance> (); > date> .> add> (> Calendar> .> DAY_OF_MONTH> , -> 0> ); > // Create filters> > Filters> > filters> = > Filters> .> builder> () .> kinds> (> kinds> ) > // Kind 1 is text note events (global posts)> .> since> (> date> .> getTimeInMillis> () / > 1000> ) > // Get posts from now> .> build> (); > // Create the NIP01 instance and set the sender identity> > var> > nip01> = > new> > NIP01> <>(> SENDER> ); > // Explicitly set the sender identity here> > EventNostr> > eventNostr> = > nip01> .> setRelays> (> RELAYS> ); > eventNostr> .> send> (> filters> , > "global-posts"> ); > Nostr> > nostr> = > new> > Nostr> (); > nostr> .> setRelays> (> RELAYS> ); > // Wait for events (adjust timing as necessary)> > Thread> .> sleep> (> 5000> ); } > private> > static> > void> > stop> (> ExecutorService> > executor> ) { > try> { > executor> .> shutdown> (); > executor> .> awaitTermination> (> 60> , > TimeUnit> .> SECONDS> ); } > catch> (> InterruptedException> > e> ) { > log> .> log> (> Level> .> SEVERE> , > "termination interrupted"> ); } > finally> { > if> (!> executor> .> isTerminated> ()) { > log> .> log> (> Level> .> SEVERE> , > "killing non-finished tasks"> ); } > executor> .> shutdownNow> (); } }}
—
Reply to this email directly, > view it on GitHub <#180>> , or > unsubscribe <https://github.com/notifications/unsubscribe-auth/ABQMG7D35YZPGKEVCSLF5RDZ3VJC3AVCNFSM6AAAAABP7WGU26VHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZXGMZDEOBYHA>> .
You are receiving this because you are subscribed to this thread.> Message ID: > <tcheeric/nostr-java/repo-discussions/180> @> github> .> com>
|
Beta Was this translation helpful? Give feedback.
-
@maxbrito500, if functionality you require is standalone, many web socket clients exist (both chrome and postman, for example) without the need to write one. otherwise if you're looking for a programmatic java client, since nostr-java currently does not support callbacks, you can:
|
Beta Was this translation helpful? Give feedback.
-
Thank you for the reply, but it would be expected from a nostr library to get a list of texts published on relays. If you look carefully, the listener/callback functionality is already available. I will avoid using a forked version for a basic necessary feature from the library. Would prefer that other developers can use this library without scattered forks. In fact, I don't even understand the logic of pushing JDK 22 as version for handling simple JSON communication. The aim should be compatibility with Java 8 ideally so this library can be used across as many JDK versions as possible including Android. Nope, sorry. These are just awful engineering decisions like the previous difficulties to setup the library to run. My apologies for the harsh statements, but these things do end up impacting nostr adoption and need to be improved. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Britto,
You will have to customise the command handlers (ok, eose, notice etc) by _providing_ your own custom module (it will be loaded automatically here https://github.com/tcheeric/nostr-java/blob/main/nostr-java-controller%2Fsrc%2Fmain%2Fjava%2Fnostr%2Fcontroller%2Fimpl%2FApplicationControllerImpl.java#L61), then add your logic in your custom ok command handler. Alternatively, you may consider Nick's fork, which he has customised for his specific needs with superconductor. (It's also probably a much cleaner nostr-java library tbh)
Please note that this project was started, and is maintained by many volunteers for _selfish_ reasons only. If you decide to implement the feature to customise the listeners, I will happily merge it.
…--
Sent with Tuta; enjoy secure & ad-free emails:
https://tuta.com
16 Oct 2024, 01:37 by ***@***.***:
current android api (v14) uses > java 17 <https://developer.android.com/build/jdks#compileSdk>> not 8
jdk22 is required for virtual threads (used by > superconductor <https://github.com/avlo/superconductor>> , a nostr relay utilizing many nostr-java classes) but an android client application developer is not forced to use any jdk22 features beyond what they wish to support in their application.
if you're inclined to implement remaining callback (& other websocket considerations) into TextListener (uses okttp3, which is not a web socket client)- instead of leveraging spring > websocketclient <https://spring.io/guides/gs/messaging-stomp-websocket>> (jdk 17+), good luck to you.
—
Reply to this email directly, > view it on GitHub <#180 (comment)>> , or > unsubscribe <https://github.com/notifications/unsubscribe-auth/ABQMG7CW6WQOEIKNNVHOIHLZ3WRK7AVCNFSM6AAAAABP7WGU26VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOJVGMZTEMQ>> .
You are receiving this because you commented.> Message ID: > <tcheeric/nostr-java/repo-discussions/180/comments/10953322> @> github> .> com>
|
Beta Was this translation helpful? Give feedback.
-
Thanks, I will see which approach to follow. Ideally would prefer to submit code to improve this library. Today I've wrote some code to get the global posts and it is really easy: /*
* Get the global posts from a specific relay
*
* Copyright (c) Nostrium contributors
* License: Apache-2.0
*/
package online.nostrium.apps.nostr;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import java.time.Instant;
/**
* @author Brito
* @date: 2024-10-17
* @location: Portugal
*/
public class NostrGlobalPosts {
// Static method to query Nostr relay
public static void queryNostrRelay(String relayUrl, long sinceTimestamp) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(relayUrl).build();
WebSocketListener listener = new WebSocketListener() {
@Override
public void onOpen(WebSocket webSocket, Response response) {
// Send subscription request with the since filter
String subscriptionRequest = "[\"REQ\", \"subscription\", {"
+ "\"kinds\": [1]," // Kind 1 for text messages
+ "\"since\": " + sinceTimestamp
+ "}]";
webSocket.send(subscriptionRequest);
System.out.println("Subscription sent: " + subscriptionRequest);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
// Handle incoming message
System.out.println("Received message: " + text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
// Handle incoming binary message
System.out.println("Received binary message: " + bytes.hex());
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(1000, null);
System.out.println("Closing WebSocket: " + reason);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
t.printStackTrace();
}
};
// Create WebSocket connection
WebSocket webSocket = client.newWebSocket(request, listener);
// Trigger shutdown of the dispatcher's executor to clean up resources.
client.dispatcher().executorService().shutdown();
}
// Utility to convert a date string to a Unix timestamp
public static long getUnixTimestamp(String date) {
Instant instant = Instant.parse(date + "T00:00:00Z");
return instant.getEpochSecond();
}
public static void main(String[] args) {
// Example usage
String relayUrl = "wss://relay.damus.io";
long sinceTimestamp = getUnixTimestamp("2024-10-17"); // Replace with desired date
queryNostrRelay(relayUrl, sinceTimestamp);
}
} |
Beta Was this translation helpful? Give feedback.
-
@maxbrito500 , have a look at this nostr client where I implement the custom command handlers. I hope it helps. PS: I had to make some minor adjustments to nostr-java to get it to work, and these changes have not yet been deployed to the jitpack repository. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I would like to get a list of the global posts being published on a few selected servers. So far I was able to write a demo program but the text is being written to console. What is needed is to output the events through a callback event.
Question: How can this code be changed so that one method is called when a new text is published?
I've looked for an answer on the examples, but I'm not yet finding how to get the new texts. I guess maybe an example for that would help other developers too.
Many thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions