From 44e12f9d3ab9349b5363cfe01a6f264ca23a9886 Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 1 Dec 2022 19:04:05 +0000 Subject: [PATCH] updated dev article --- docs/devs/websockets.md | 55 ++----------------- .../templates/lnurldevice/index.html | 2 +- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/docs/devs/websockets.md b/docs/devs/websockets.md index 0638e4f28..9ea53a624 100644 --- a/docs/devs/websockets.md +++ b/docs/devs/websockets.md @@ -9,53 +9,10 @@ nav_order: 2 Websockets ================= -`websockets` are a great way to add a two way instant data channel between server and client. This example was taken from the `copilot` extension, we create a websocket endpoint which can be restricted by `id`, then can feed it data to broadcast to any client on the socket using the `updater(extension_id, data)` function (`extension` has been used in place of an extension name, wreplace to your own extension): +`websockets` are a great way to add a two way instant data channel between server and client. +LNbits has a useful in built websocket tool. With a websocket client connect to (obv change `somespecificid`) `wss://legend.lnbits.com/api/v1/ws/somespecificid` (you can use an online websocket tester). Now make a get to `https://legend.lnbits.com/api/v1/ws/somespecificid/somedata`. You can send data to that websocket by using `from lnbits.core.services import websocketUpdater` and the function `websocketUpdater("somespecificid", "somdata")`. -```sh -from fastapi import Request, WebSocket, WebSocketDisconnect - -class ConnectionManager: - def __init__(self): - self.active_connections: List[WebSocket] = [] - - async def connect(self, websocket: WebSocket, extension_id: str): - await websocket.accept() - websocket.id = extension_id - self.active_connections.append(websocket) - - def disconnect(self, websocket: WebSocket): - self.active_connections.remove(websocket) - - async def send_personal_message(self, message: str, extension_id: str): - for connection in self.active_connections: - if connection.id == extension_id: - await connection.send_text(message) - - async def broadcast(self, message: str): - for connection in self.active_connections: - await connection.send_text(message) - - -manager = ConnectionManager() - - -@extension_ext.websocket("/ws/{extension_id}", name="extension.websocket_by_id") -async def websocket_endpoint(websocket: WebSocket, extension_id: str): - await manager.connect(websocket, extension_id) - try: - while True: - data = await websocket.receive_text() - except WebSocketDisconnect: - manager.disconnect(websocket) - - -async def updater(extension_id, data): - extension = await get_extension(extension_id) - if not extension: - return - await manager.send_personal_message(f"{data}", extension_id) -``` Example vue-js function for listening to the websocket: @@ -67,16 +24,16 @@ initWs: async function () { document.domain + ':' + location.port + - '/extension/ws/' + - self.extension.id + '/api/v1/ws/' + + self.item.id } else { localUrl = 'ws://' + document.domain + ':' + location.port + - '/extension/ws/' + - self.extension.id + '/api/v1/ws/' + + self.item.id } this.ws = new WebSocket(localUrl) this.ws.addEventListener('message', async ({data}) => { diff --git a/lnbits/extensions/lnurldevice/templates/lnurldevice/index.html b/lnbits/extensions/lnurldevice/templates/lnurldevice/index.html index 04d645830..b2165590e 100644 --- a/lnbits/extensions/lnurldevice/templates/lnurldevice/index.html +++ b/lnbits/extensions/lnurldevice/templates/lnurldevice/index.html @@ -657,7 +657,7 @@ lnurlValueFetch: function (lnurl, switchId) { this.lnurlValue = lnurl this.websocketConnector( - 'wss://' + window.location.host + '/lnurldevice/ws/' + switchId + 'wss://' + window.location.host + '/api/v1/ws/' + switchId ) }, addSwitch: function () {