@ethersphere/bee-js
Version:
Javascript client for Bee
43 lines • 1.29 kB
JavaScript
import { System } from 'cafe-utility';
import WebSocket from 'isomorphic-ws';
import { prepareRequestHeaders } from "../utils/headers.js";
import { http } from "../utils/http.js";
const endpoint = 'pss';
/**
* Send to recipient or target with Postal Service for Swarm
*
* @param requestOptions Options for making requests
* @param topic Topic name
* @param target Target message address prefix
* @param data
* @param postageBatchId Postage BatchId that will be assigned to sent message
* @param recipient Recipient public key
*
*/
export async function send(requestOptions, topic, target, data, postageBatchId, recipient) {
await http(requestOptions, {
method: 'post',
url: `${endpoint}/send/${topic}/${target}`,
data,
responseType: 'json',
params: {
recipient
},
headers: prepareRequestHeaders(postageBatchId)
});
}
/**
* Subscribe for messages on the given topic
*
* @param url Bee node URL
* @param topic Topic name
*/
export function subscribe(url, topic, headers) {
const wsUrl = url.replace(/^http/i, 'ws');
if (System.whereAmI() === 'browser') {
return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`);
}
return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic.toHex()}`, {
headers
});
}