lnbits/docs/devs/websockets.md

45 lines
1.3 KiB
Markdown
Raw Normal View History

2022-10-06 16:41:06 +00:00
---
layout: default
parent: For developers
title: Websockets
nav_order: 2
---
Websockets
=================
2022-12-01 19:04:05 +00:00
`websockets` are a great way to add a two way instant data channel between server and client.
2022-10-06 16:41:06 +00:00
2022-12-01 19:04:05 +00:00
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")`.
2022-10-06 16:41:06 +00:00
Example vue-js function for listening to the websocket:
```
initWs: async function () {
if (location.protocol !== 'http:') {
localUrl =
'wss://' +
document.domain +
':' +
location.port +
2022-12-01 19:04:05 +00:00
'/api/v1/ws/' +
self.item.id
2022-10-06 16:41:06 +00:00
} else {
localUrl =
'ws://' +
document.domain +
':' +
location.port +
2022-12-01 19:04:05 +00:00
'/api/v1/ws/' +
self.item.id
2022-10-06 16:41:06 +00:00
}
this.ws = new WebSocket(localUrl)
this.ws.addEventListener('message', async ({data}) => {
const res = JSON.parse(data.toString())
console.log(res)
})
},
2022-10-06 16:44:04 +00:00
```