nostrver-se / nostr-php

PHP helper library for Nostr https://nostr-php.dev
https://nostr-php.dev
MIT License
53 stars 15 forks source link

Uncaught WebSocket\ConnectionException #26

Closed pjv closed 11 months ago

pjv commented 1 year ago

I’m building a wordpress plugin and ran across this fatal error when a relay sent a 503 connection error.

I’ll try to add a try… catch in my code to work around this, but I’m posting this here in case you think it would be useful to catch errors like this at the library level. It seems to me that would be better.

[27-Mar-2023 11:01:47 UTC] PHP Fatal error:  Uncaught WebSocket\ConnectionException: Connection to 'wss://nostr-pub.wellorder.net' failed: Server sent invalid upgrade response: HTTP/1.0 503 Service Unavailable
cache-control: no-cache
content-type: text/html

 in /var/www/html/wp-content/plugins/wp-nostr/vendor/textalk/websocket/lib/Client.php:441
Stack trace:
#0 /var/www/html/wp-content/plugins/wp-nostr/vendor/textalk/websocket/lib/Client.php(169): WebSocket\Client->connect()
#1 /var/www/html/wp-content/plugins/wp-nostr/vendor/textalk/websocket/lib/Client.php(130): WebSocket\Client->send()
#2 /var/www/html/wp-content/plugins/wp-nostr/vendor/swentel/nostr-php/src/Relay/Relay.php(46): WebSocket\Client->text()
#3 /var/www/html/wp-content/plugins/wp-nostr/classes/class-wp-nostr.php(155): swentel\nostr\Relay\Relay->send()
#4 /var/www/html/wp-content/plugins/wp-nostr/classes/class-wp-nostr.php(97): WP_Nostr->send_note()
#5 /var/www/html/wp-includes/class-wp-hook.php(308): WP_Nostr->post_note()
#6 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters()
#7 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action()
#8 /var/www/html/wp-admin/admin-ajax.php(188): do_action()
#9 {main}
  thrown in /var/www/html/wp-content/plugins/wp-nostr/vendor/textalk/websocket/lib/Client.php on line 441
georgkaser commented 1 year ago

I have also an exception: Without try catch $result = $relay->send(); is not working. With try catch it works, posts the "Hello world" String to the relay - but with an exception "Empty read; connection dead?"

What I am doing wrong?

georgkaser commented 1 year ago

It seems that it's not needed to close the connection in the function send(): After commenting $client->close(); it works fine with no exception. Is the connection closing automatically?

swentel commented 1 year ago

Interesting .. it looks like adding a try/catch in the library makes sense. But I'd throw our own exception then would make sense in a way I think. Let me think about it some more.

(note: I'm on holiday until the 17th, so a bit slow at the moment)

Sebastix commented 11 months ago

@georgkaser Is this an issue for you? A small change is just made on how the connection to a relay is closed: https://github.com/swentel/nostr-php/pull/46