Merge pull request #3333 from wralith/update-js-sdk

Update javascript snippets
This commit is contained in:
Hannah Hunter 2023-04-13 10:04:41 -04:00 committed by GitHub
commit fa2416a97c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 85 additions and 42 deletions

View File

@ -270,7 +270,11 @@ const daprHost = "127.0.0.1";
async function sendOrder(orderId) { async function sendOrder(orderId) {
const BINDING_NAME = "checkout"; const BINDING_NAME = "checkout";
const BINDING_OPERATION = "create"; const BINDING_OPERATION = "create";
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
//Using Dapr SDK to invoke output binding //Using Dapr SDK to invoke output binding
const result = await client.binding.send(BINDING_NAME, BINDING_OPERATION, orderId); const result = await client.binding.send(BINDING_NAME, BINDING_OPERATION, orderId);
console.log("Sending message: " + orderId); console.log("Sending message: " + orderId);

View File

@ -237,7 +237,15 @@ start().catch((e) => {
}); });
async function start() { async function start() {
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.HTTP); const server = new DaprServer({
serverHost,
serverPort,
communicationProtocol: CommunicationProtocolEnum.HTTP,
clientOptions: {
daprHost,
daprPort,
}
});
await server.binding.receive('checkout', async (orderId) => console.log(`Received Message: ${JSON.stringify(orderId)}`)); await server.binding.receive('checkout', async (orderId) => console.log(`Received Message: ${JSON.stringify(orderId)}`));
await server.startServer(); await server.startServer();
} }

View File

@ -355,13 +355,15 @@ start().catch((e) => {
}); });
async function start(orderId) { async function start(orderId) {
const server = new DaprServer( const server = new DaprServer({
serverHost, serverHost,
serverPort, serverPort,
communicationProtocol: CommunicationProtocolEnum.HTTP,
clientOptions: {
daprHost, daprHost,
process.env.DAPR_HTTP_PORT, daprPort: process.env.DAPR_HTTP_PORT,
CommunicationProtocolEnum.HTTP },
); });
//Subscribe to a topic //Subscribe to a topic
await server.pubsub.subscribe("order-pub-sub", "orders", async (orderId) => { await server.pubsub.subscribe("order-pub-sub", "orders", async (orderId) => {
console.log(`Subscriber received: ${JSON.stringify(orderId)}`) console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
@ -625,7 +627,11 @@ var main = function() {
async function start(orderId) { async function start(orderId) {
const PUBSUB_NAME = "order-pub-sub" const PUBSUB_NAME = "order-pub-sub"
const TOPIC_NAME = "orders" const TOPIC_NAME = "orders"
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP
});
console.log("Published data:" + orderId) console.log("Published data:" + orderId)
//Using Dapr SDK to publish a topic //Using Dapr SDK to publish a topic
await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId); await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId);

View File

@ -394,13 +394,20 @@ import { DaprServer } from "@dapr/dapr";
const pubSubName = "orderPubSub"; const pubSubName = "orderPubSub";
const topic = "topicbulk"; const topic = "topicbulk";
const DAPR_HOST = process.env.DAPR_HOST || "127.0.0.1"; const daprHost = process.env.DAPR_HOST || "127.0.0.1";
const DAPR_HTTP_PORT = process.env.DAPR_HTTP_PORT || "3502"; const daprPort = process.env.DAPR_HTTP_PORT || "3502";
const SERVER_HOST = process.env.SERVER_HOST || "127.0.0.1"; const serverHost = process.env.SERVER_HOST || "127.0.0.1";
const SERVER_PORT = process.env.APP_PORT || 5001; const serverPort = process.env.APP_PORT || 5001;
async function start() { async function start() {
const server = new DaprServer(SERVER_HOST, SERVER_PORT, DAPR_HOST, DAPR_HTTP_PORT); const server = new DaprServer({
serverHost,
serverPort,
clientOptions: {
daprHost,
daprPort,
},
});
// Publish multiple messages to a topic with default config. // Publish multiple messages to a topic with default config.
await client.pubsub.bulkSubscribeWithDefaultConfig(pubSubName, topic, (data) => console.log("Subscriber received: " + JSON.stringify(data))); await client.pubsub.bulkSubscribeWithDefaultConfig(pubSubName, topic, (data) => console.log("Subscriber received: " + JSON.stringify(data)));

View File

@ -218,7 +218,11 @@ import { DaprClient, HttpMethod, CommunicationProtocolEnum } from '@dapr/dapr';
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
async function main() { async function main() {
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
const SECRET_STORE_NAME = "localsecretstore"; const SECRET_STORE_NAME = "localsecretstore";
//Using Dapr SDK to get a secret //Using Dapr SDK to get a secret
var secret = await client.secret.get(SECRET_STORE_NAME, "secret"); var secret = await client.secret.get(SECRET_STORE_NAME, "secret");

View File

@ -347,7 +347,12 @@ var main = function() {
} }
async function start(orderId) { async function start(orderId) {
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost: daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP
});
//Using Dapr SDK to invoke a method //Using Dapr SDK to invoke a method
const result = await client.invoker.invoke('checkoutservice' , "checkout/" + orderId , HttpMethod.GET); const result = await client.invoker.invoke('checkoutservice' , "checkout/" + orderId , HttpMethod.GET);
console.log("Order requested: " + orderId); console.log("Order requested: " + orderId);

View File

@ -266,7 +266,11 @@ var main = function() {
} }
async function start(orderId) { async function start(orderId) {
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and get state //Using Dapr SDK to save and get state
await client.state.save(STATE_STORE_NAME, [ await client.state.save(STATE_STORE_NAME, [
@ -483,7 +487,12 @@ const daprHost = "127.0.0.1";
var main = function() { var main = function() {
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and get state //Using Dapr SDK to save and get state
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
await client.state.delete(STATE_STORE_NAME, "order_1"); await client.state.delete(STATE_STORE_NAME, "order_1");
} }
@ -630,7 +639,12 @@ var main = function() {
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
var orderId = 100; var orderId = 100;
//Using Dapr SDK to save and retrieve multiple states //Using Dapr SDK to save and retrieve multiple states
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
await client.state.save(STATE_STORE_NAME, [ await client.state.save(STATE_STORE_NAME, [
{ {
key: "order_1", key: "order_1",
@ -870,7 +884,12 @@ var main = function() {
} }
async function start(orderId) { async function start(orderId) {
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient({
daprHost,
daprPort: process.env.DAPR_HTTP_PORT,
communicationProtocol: CommunicationProtocolEnum.HTTP,
});
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and retrieve multiple states //Using Dapr SDK to save and retrieve multiple states
await client.state.transaction(STATE_STORE_NAME, [ await client.state.transaction(STATE_STORE_NAME, [

View File

@ -273,7 +273,7 @@ dapr run --app-id checkout --app-protocol http --dapr-http-port 3500 --resources
In the `checkout` publisher service, we're publishing the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop: In the `checkout` publisher service, we're publishing the orderId message to the Redis instance called `orderpubsub` [(as defined in the `pubsub.yaml` component)]({{< ref "#pubsubyaml-component-file" >}}) and topic `orders`. As soon as the service starts, it publishes in a loop:
```js ```js
const client = new DaprClient(DAPR_HOST, DAPR_HTTP_PORT); const client = new DaprClient();
await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order); await client.pubsub.publish(PUBSUB_NAME, PUBSUB_TOPIC, order);
console.log("Published data: " + JSON.stringify(order)); console.log("Published data: " + JSON.stringify(order));

View File

@ -177,29 +177,19 @@ dapr run --app-id order-processor --resources-path ../../../resources/ -- npm ru
The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop. The `order-processor` service writes, reads, and deletes an `orderId` key/value pair to the `statestore` instance [defined in the `statestore.yaml` component]({{< ref "#statestoreyaml-component-file" >}}). As soon as the service starts, it performs a loop.
```js ```js
const client = new DaprClient(DAPR_HOST, DAPR_HTTP_PORT); const client = new DaprClient()
// Save state into the state store // Save state into a state store
client.state.save(STATE_STORE_NAME, [ await client.state.save(DAPR_STATE_STORE_NAME, state)
{ console.log("Saving Order: ", order)
key: orderId.toString(),
value: order
}
]);
console.log("Saving Order: ", order);
// Get state from the state store // Get state from a state store
var result = client.state.get(STATE_STORE_NAME, orderId.toString()); const savedOrder = await client.state.get(DAPR_STATE_STORE_NAME, order.orderId)
result.then(function(val) { console.log("Getting Order: ", savedOrd
console.log("Getting Order: ", val);
});
// Delete state from the state store
client.state.delete(STATE_STORE_NAME, orderId.toString());
result.then(function(val) {
console.log("Deleting Order: ", val);
});
// Delete state from the state store
await client.state.delete(DAPR_STATE_STORE_NAME, order.orderId)
console.log("Deleting Order: ", order)
``` ```
### Step 3: View the order-processor outputs ### Step 3: View the order-processor outputs