Skip to content

Instantly share code, notes, and snippets.

@vapopov
Last active August 7, 2019 17:03
Show Gist options
  • Save vapopov/d4fd2bede07b779255ecbe9a1c9efc00 to your computer and use it in GitHub Desktop.
Save vapopov/d4fd2bede07b779255ecbe9a1c9efc00 to your computer and use it in GitHub Desktop.
Sample of connections to web-sockete
"use strict";
const axios = require("axios");
const BASE_URL = "hub-testnet.lightningpeach.com";
const WebSocket = require("ws");
const main = async () => {
try {
const signUpRequestOptions = {
url: `https://${BASE_URL}/api/v2/auth/signup`,
method: "POST",
headers: {
"Content-Type": "application/json",
// "Authorization": "Bearer token"
},
data: {
email: "test@gmail.com",
password: "Qwer1234",
}
};
const response = await axios(signUpRequestOptions);
} catch (e) {
// console.log(e);
}
try {
const signUpRequestOptions = {
url: `https://${BASE_URL}/api/v2/auth/signin`,
method: "POST",
headers: {
"Content-Type": "application/json",
// "Authorization": "Bearer token"
},
data: {
email: "test@gmail.com",
password: "Qwer1234",
}
};
const response = await axios(signUpRequestOptions);
const connectionUrl = `wss://${BASE_URL}/api/v2/user/messages/subscribe`;
console.log(response.data.merchant_id);
console.log(connectionUrl);
const ws = new WebSocket(connectionUrl, ["Bearer", response.data.access_token]);
console.log("Here 1");
ws.on("message", function incoming(data) {
console.log("received message");
console.log(data);
});
console.log("Here 2");
ws.on("open", function open() {
ws.send("{}");
console.log("Here 3");
});
} catch (e) {
console.log(e)
}
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment