Skip to content

Commit 44e12f9

Browse files
committed
updated dev article
1 parent 9a50f41 commit 44e12f9

File tree

2 files changed

+7
-50
lines changed

2 files changed

+7
-50
lines changed

docs/devs/websockets.md

+6-49
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,10 @@ nav_order: 2
99
Websockets
1010
=================
1111

12-
`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):
12+
`websockets` are a great way to add a two way instant data channel between server and client.
1313

14+
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")`.
1415

15-
```sh
16-
from fastapi import Request, WebSocket, WebSocketDisconnect
17-
18-
class ConnectionManager:
19-
def __init__(self):
20-
self.active_connections: List[WebSocket] = []
21-
22-
async def connect(self, websocket: WebSocket, extension_id: str):
23-
await websocket.accept()
24-
websocket.id = extension_id
25-
self.active_connections.append(websocket)
26-
27-
def disconnect(self, websocket: WebSocket):
28-
self.active_connections.remove(websocket)
29-
30-
async def send_personal_message(self, message: str, extension_id: str):
31-
for connection in self.active_connections:
32-
if connection.id == extension_id:
33-
await connection.send_text(message)
34-
35-
async def broadcast(self, message: str):
36-
for connection in self.active_connections:
37-
await connection.send_text(message)
38-
39-
40-
manager = ConnectionManager()
41-
42-
43-
@extension_ext.websocket("/ws/{extension_id}", name="extension.websocket_by_id")
44-
async def websocket_endpoint(websocket: WebSocket, extension_id: str):
45-
await manager.connect(websocket, extension_id)
46-
try:
47-
while True:
48-
data = await websocket.receive_text()
49-
except WebSocketDisconnect:
50-
manager.disconnect(websocket)
51-
52-
53-
async def updater(extension_id, data):
54-
extension = await get_extension(extension_id)
55-
if not extension:
56-
return
57-
await manager.send_personal_message(f"{data}", extension_id)
58-
```
5916

6017
Example vue-js function for listening to the websocket:
6118

@@ -67,16 +24,16 @@ initWs: async function () {
6724
document.domain +
6825
':' +
6926
location.port +
70-
'/extension/ws/' +
71-
self.extension.id
27+
'/api/v1/ws/' +
28+
self.item.id
7229
} else {
7330
localUrl =
7431
'ws://' +
7532
document.domain +
7633
':' +
7734
location.port +
78-
'/extension/ws/' +
79-
self.extension.id
35+
'/api/v1/ws/' +
36+
self.item.id
8037
}
8138
this.ws = new WebSocket(localUrl)
8239
this.ws.addEventListener('message', async ({data}) => {

lnbits/extensions/lnurldevice/templates/lnurldevice/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ <h6 class="text-subtitle1 q-my-none">
657657
lnurlValueFetch: function (lnurl, switchId) {
658658
this.lnurlValue = lnurl
659659
this.websocketConnector(
660-
'wss://' + window.location.host + '/lnurldevice/ws/' + switchId
660+
'wss://' + window.location.host + '/api/v1/ws/' + switchId
661661
)
662662
},
663663
addSwitch: function () {

0 commit comments

Comments
 (0)