updated dev article
This commit is contained in:
parent
9a50f4187e
commit
44e12f9d3a
|
@ -9,53 +9,10 @@ nav_order: 2
|
||||||
Websockets
|
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:
|
Example vue-js function for listening to the websocket:
|
||||||
|
|
||||||
|
@ -67,16 +24,16 @@ initWs: async function () {
|
||||||
document.domain +
|
document.domain +
|
||||||
':' +
|
':' +
|
||||||
location.port +
|
location.port +
|
||||||
'/extension/ws/' +
|
'/api/v1/ws/' +
|
||||||
self.extension.id
|
self.item.id
|
||||||
} else {
|
} else {
|
||||||
localUrl =
|
localUrl =
|
||||||
'ws://' +
|
'ws://' +
|
||||||
document.domain +
|
document.domain +
|
||||||
':' +
|
':' +
|
||||||
location.port +
|
location.port +
|
||||||
'/extension/ws/' +
|
'/api/v1/ws/' +
|
||||||
self.extension.id
|
self.item.id
|
||||||
}
|
}
|
||||||
this.ws = new WebSocket(localUrl)
|
this.ws = new WebSocket(localUrl)
|
||||||
this.ws.addEventListener('message', async ({data}) => {
|
this.ws.addEventListener('message', async ({data}) => {
|
||||||
|
|
|
@ -657,7 +657,7 @@
|
||||||
lnurlValueFetch: function (lnurl, switchId) {
|
lnurlValueFetch: function (lnurl, switchId) {
|
||||||
this.lnurlValue = lnurl
|
this.lnurlValue = lnurl
|
||||||
this.websocketConnector(
|
this.websocketConnector(
|
||||||
'wss://' + window.location.host + '/lnurldevice/ws/' + switchId
|
'wss://' + window.location.host + '/api/v1/ws/' + switchId
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
addSwitch: function () {
|
addSwitch: function () {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user