Git Product home page Git Product logo

Comments (4)

sgeisler avatar sgeisler commented on September 12, 2024

This will also reduce the load on the backend if users don't have to poll all the time. Most real node backends support some form of notification on payment finalization. For the ones that don't support that there could be a central thread polling all open payments and sending notifications instead of every request causing a separate poll.

from lnbits.

arcbtc avatar arcbtc commented on September 12, 2024

WebSockets need to happen at some point, this is as good an excuse as any.
Linking LNbits and BTCPay server is an exciting concept

from lnbits.

Kukks avatar Kukks commented on September 12, 2024

WebSockets need to happen at some point, this is as good an excuse as any.
Linking LNbits and BTCPay server is an exciting concept

Yes, I could integrate LNBits as if it was another lightning node in our lightning abstraction. Someone offering a BTCPay server to multiple merchants could then host LNBits alongside and use that as a way for them to accept lightning via their node!

Will need the websocket support and multiarch docker images though

from lnbits.

fiatjaf avatar fiatjaf commented on September 12, 2024

Hey, just a heads up: LNbits now has an (still undocumented while we make sure it works) SSE endpoint that you can listen to to get an event every time an invoice payment is received (it listens for notifications in the background from each of the funding sources -- except lntxbot), matches the incoming payments to each LNbits wallet and dispatches the event there.

curl https://lnbits.example/api/v1/payments/sse -H 'x-api-key: yourkey' should work. It will give you something like this:

curl 'http://localhost:5000/api/v1/payments/sse' -H 'x-api-key: 4b0518a9a640af02b5165fd9879a756c'
event: keepalive

event: payment
data: {"checking_id": "UeEG32TSDuKUzsxwUJupgX0Nlf8Ix9aElrDPGcmQzUU=", "pending": 0, "amount": 600000, "fee": 0, "memo": "an invoice", "time": 1602288389, "bolt11": "lnbcrt60n1p0cp7c9pp528ssdhmy6g8w99xwe3c9pxafs97sm90lprradpykkr83njvse4zsdqzyccqzpgxqzjcsp52439y3p0sg8mqvgrr5tfx56prq59nrmmq52cqjv09wr9c3kw35yq9qy9qsqfjv5egvze5dtl7n0dg24f533xdvvxm2w0u9l5pl3j79fvfknc05q02lxtggtd3rqsrgu6wyetyt0tgdhv78d38s4qljmvar00hux2dspcqq0k7", "preimage": "0000000000000000000000000000000000000000000000000000000000000000", "payment_hash": "51e106df64d20ee294cecc70509ba9817d0d95ff08c7d68496b0cf19c990cd45", "extra": {}, "wallet_id": "8fbefbdba16c4dd19afa641ebd51fd5e"}

event: keepalive

event: keepalive

I chose an SSE stream because it seems to be lighter and simpler to implement on every side, but WebSockets or Webhooks are also possible to do.

The code is here if anyone is curious:

@core_app.route("/api/v1/payments/sse", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_payments_sse():
g.db.close()
this_wallet_id = g.wallet.id
send_payment, receive_payment = trio.open_memory_channel(0)
print("adding sse listener", send_payment)
sse_listeners.append(send_payment)
send_event, receive_event = trio.open_memory_channel(0)
async def payment_received() -> None:
async for payment in receive_payment:
if payment.wallet_id == this_wallet_id:
await send_event.send(("payment", payment))
async def repeat_keepalive():
await trio.sleep(1)
while True:
await send_event.send(("keepalive", ""))
await trio.sleep(25)
g.nursery.start_soon(payment_received)
g.nursery.start_soon(repeat_keepalive)
async def send_events():
try:
async for typ, data in receive_event:
message = [f"event: {typ}".encode("utf-8")]
if data:
jdata = json.dumps(dict(data._asdict(), pending=False))
message.append(f"data: {jdata}".encode("utf-8"))
yield b"\n".join(message) + b"\r\n\r\n"
except trio.Cancelled:
return
response = await make_response(
send_events(),
{
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive",
"Transfer-Encoding": "chunked",
},
)
response.timeout = None
return response

from lnbits.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.