Relay transmits POST messages from server throught websockets (socket.io actually) to clients and websockets messages throught POST back to server.
On the client side, it is a common problem of keeping data up-to-date. Sometimes, implementing websokets might be inconvenient due to technological stack or the lack of experience. With Relay you can send data from server to client with a familiar HTTP POST request.
-
Install Relay
git clone https://github.com/ivanrussu/relay cd ./relay npm install cp config.example.js config.js npm run serve
-
Prepare a webhook. Webhook is used to control subscriptions and getting information from Relay. Webhooks are called as form-data POST requests and contain
api_key
,event
anddata
fields.event
field determines the type of a webhook. It might beconnected
which contains no data and triggers every time a client connects to your link (you will get your link in the next step). When a client is trying to subscribe to a channelsubscribe
event triggers, in this casedata
contains array of strings inchannels
field and any object inpayload
. For example, you can usepayload
to authenticate clients. The last type of webhooks isrequest
with any object asdata
. It is called when a client makes a request to your server. -
Create an API Key. API Key is used to authenticate requests between your server and Relay. To create one, use command
npm run make api-key
. Answer questions and you will obtain API key as well as the link. Keep the key safe. Share the link with frontend. -
Send a message. Once the clients are ready to receive messages, simply make POST request like
curl --location --request POST 'https://relay.example.com/api/v1/broadcast' \ --header 'Content-Type: application/json' \ --data-raw '{ "api_key": "YOUR_API_KEY", "channel": "notifications", "data": { "event": "new_message", "user_id": 700695, "message": "Hello, how are you?" } }'