Developing Websocket Servers For Stellar
Last updated
Last updated
This will be a guide on how someone could create a custom websocket server compatible with stellar to be used with the websocket page!
This guide is meant to help developers looking to create websocket servers for stellar users to connect to within the bot. We have decided to add this feature because it was highly requested and it is honestly a great idea. We would love to see many cook groups / monitor providers adopt this and provide feedback so we can improve it.
This guide will go over these main topics...
How do external websocket servers work with stellar?
How do I build a websocket server stellar can connect to?
How do I implement authentication?
Currently, stellar users can connect to websocket servers via a tab in the settings menu which looks like this.
Websocket servers users connect to act identically to Stellar's in house serverside monitoring websocket servers. This diagram may help better explain how everything works together.
Building a websocket server is a relatively simple task, you can take a look at the examples provided a couple sections below this one if you need some help.
The main three concerns here are...
What language do I use?
How do I notify my connected users when a product restocks?
How do I make sure only authenticated users can connect?
Your websocket server may be written in any language as long as it allows stellar users to connect via a url (has an open port or reverse proxy to accept traffic on 80 (ws://) or 433 (wss://) and redirect to your app such as nginx), and it has the ability to send stringified JSON payloads (schema below) to all connected users when a product restocks.
To notify all users connected to your websocket server when a product restocks, you need to send stringified JSON payloads to stellar with the following format.
This is a question we have been asked about a lot, but the answer is relatively simple. You can pass in a key with a query string in the ws url, access it however your preferred ws library will allow you to, keep an array of all connected keys, upon a new connection first check if the key attempting to connect is in the array of keys, then check if the key is valid, if it is add the key to the array of connected keys. This makes sure unauthenticated users cant connect and the same key can't be used multiple times. You can see an example of this method being implemented in the examples repo.