Replying to Avatar Cesar Dias

Working with websocketSubjects has been very easy and flexible, I can even push messages before the websocket was connected and the subject will buffer after connected.

webSocketSubjects just like the other subjects are multicasted.

this will only open 1 connection no matter how many subscriptions you have

```ts

const relay = webSocketSubject('wss://relay1.com')

relay.subscribe()

relay.subscribe()

```

now creating multiple webSocketSubjects it will open multiple connections.

webSocketSubjects multiplex will make a unicast subscription, this will make multiple nostr subscriptions in the same connection.

```ts

const relay = webSocketSubject('wss://relay1.com')

const sub = relay.multiplex(() => ["REQ", , ], () => ['CLOSE', ], (msg) => msg msg[1] === )

sub.subscribe()

sub.subscribe()

```

You can pass the webSocketSubject as you wish as it's also a observable, I store relays as webSocketSubjects in a Map in the Pool which doesn't do anything special, just blacklist relays with errors so it doesn't try to connect again.

Current my core/pool is doing the relay subscription which is incorrect, the consumer should be responsible to initiate the relay connection because he could put any policy mechanism for a particular relay, something like a rxjs retry to reconnect or some auth policy, currently my pool exposes a open callback and we could do something like this (pseudo code)

I am still working on auth and trying to think about some flexible way to do it, I'm a little bit hesitant to replay old messages after a a successful auth as I need to store these messages somewhere, but here's a small authenticator (which I think I should rename to createAuthPolicy or something) what takes a pool, a signer a options like whitelist which is an observable as the user can whitelist a observable after the auth was received.

Still half backed as I am still testing locally

nostr:npub1cesrkrcuelkxyhvupzm48e8hwn4005w0ya5jyvf9kh75mfegqx0q4kt37c nostr:npub1jlrs53pkdfjnts29kveljul2sm0actt6n8dxrrzqcersttvcuv3qdjynqn I did a little more work on my implementation and id like to get your feedback. Its still missing re-connection logic but personally I really excited with how it turned out

https://github.com/hzrd149/applesauce/tree/master/packages/relay

Reply to this note

Please Login to reply.

Discussion

That's a slick interface, I like it a lot. The main reason I wouldn't use this implementation is that a lot of the policy is baked in, which might need to be configurable by the application. I have a bunch of policy stuff here: https://github.com/coracle-social/welshman/blob/net2/packages/net2/src/policy.ts which is mostly reasonable defaults, but if users wanted to configure auth policy per relay I don't see how that could be done in applesauce. Also, why no`subscribe` ?

My idea is that it could be built on top if the relay communication is flexible enough. most nostr libraries build the auth, limitations, and connection policies at the "new WebSocket()" level, but that makes it difficult to add additional types of behavior that a different client might want later

For example you could implement a relay blacklist at the websocket level so that it would throw an error (or make it look offline) if the client connected to a blacklisted relay. or you could extend that relay pool class to ensure it never even tries to connect to blacklisted relays. all of this is just a theory though, I still have to put it into practice to see if it works

Ah, nice, that's the opposite approach to what I'm doing but it makes plenty of sense

My goal is to build and maintain 20+ small nostr apps, so I need something that will work for all different kinds of uses :)