mirror of https://github.com/dapr/docs.git
				
				
				
			Added full code snippets of pub sub
Signed-off-by: Amulya Varote <amulyavarote@Amulyas-MacBook-Pro.local>
This commit is contained in:
		
							parent
							
								
									ec61461b00
								
							
						
					
					
						commit
						e1bbdaa9ed
					
				|  | @ -262,8 +262,11 @@ Below are code examples that leverage Dapr SDKs to subscribe to a topic. | |||
| {{% codetab %}} | ||||
| 
 | ||||
| ```csharp | ||||
| 
 | ||||
| //dependencies  | ||||
| using System.Collections.Generic; | ||||
| using System.Threading.Tasks; | ||||
| using System; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Dapr; | ||||
| using Dapr.Client; | ||||
| 
 | ||||
|  | @ -351,19 +354,25 @@ import io.dapr.client.domain.CloudEvent; | |||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| ======= | ||||
| ======= | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> 0e83af7a (Added pub sub documentation) | ||||
| ======= | ||||
| >>>>>>> fbadd23a (Modified based on the review comments - 1) | ||||
| ======= | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| import reactor.core.publisher.Mono; | ||||
| 
 | ||||
| //code | ||||
|  | @ -403,6 +412,7 @@ from dapr.ext.grpc import App | |||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| ======= | ||||
| import logging | ||||
| import json | ||||
|  | @ -413,6 +423,10 @@ import json | |||
| import logging | ||||
| import json | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| import logging | ||||
| import json | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| 
 | ||||
| #code | ||||
| app = App() | ||||
|  | @ -648,6 +662,7 @@ Below are code examples that leverage Dapr SDKs to publish a topic. | |||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| using Dapr.Client; | ||||
| ======= | ||||
| using System; | ||||
|  | @ -672,6 +687,16 @@ using Dapr.Client; | |||
| using Microsoft.AspNetCore.Mvc; | ||||
| using System.Threading; | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Net.Http; | ||||
| using System.Net.Http.Headers; | ||||
| using System.Threading.Tasks; | ||||
| using Dapr.Client; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using System.Threading; | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| 
 | ||||
| //code | ||||
| namespace EventService | ||||
|  | @ -680,6 +705,7 @@ namespace EventService | |||
|     { | ||||
|         static async Task Main(string[] args) | ||||
|         { | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
|           string PUBSUB_NAME = "order_pub_sub"; | ||||
|           string TOPIC_NAME = "orders"; | ||||
|  | @ -699,6 +725,8 @@ namespace EventService | |||
|           await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken); | ||||
|           Console.WriteLine("Published data: " + orderId); | ||||
| ======= | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
|            string PUBSUB_NAME = "order_pub_sub"; | ||||
|            string TOPIC_NAME = "orders"; | ||||
|            while(true) { | ||||
|  | @ -712,7 +740,10 @@ namespace EventService | |||
|                 await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken); | ||||
|                 Console.WriteLine("Published data: " + orderId); | ||||
| 		        } | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | @ -737,23 +768,30 @@ import static java.util.Collections.singletonMap; | |||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| ======= | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| >>>>>>> 0e83af7a (Added pub sub documentation) | ||||
| ======= | ||||
| >>>>>>> fbadd23a (Modified based on the review comments - 1) | ||||
| ======= | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import java.util.Random; | ||||
| import java.util.concurrent.TimeUnit; | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| 
 | ||||
| //code | ||||
| @SpringBootApplication | ||||
| public class OrderProcessingServiceApplication { | ||||
| 
 | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
|   public static void main(String[] args) throws InterruptedException{ | ||||
|     String MESSAGE_TTL_IN_SECONDS = "1000"; | ||||
|  | @ -782,6 +820,30 @@ public class OrderProcessingServiceApplication { | |||
| } | ||||
| <<<<<<< HEAD | ||||
| 
 | ||||
| ======= | ||||
| 	private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class); | ||||
| 
 | ||||
| 	public static void main(String[] args) throws InterruptedException{ | ||||
| 		String MESSAGE_TTL_IN_SECONDS = "1000"; | ||||
| 		String TOPIC_NAME = "orders"; | ||||
| 		String PUBSUB_NAME = "order_pub_sub"; | ||||
| 
 | ||||
| 		while(true) { | ||||
| 			TimeUnit.MILLISECONDS.sleep(5000); | ||||
| 			Random random = new Random(); | ||||
| 			int orderId = random.nextInt(1000-1) + 1; | ||||
| 			DaprClient client = new DaprClientBuilder().build(); | ||||
|       //Using Dapr SDK to publish a topic | ||||
| 			client.publishEvent( | ||||
| 					PUBSUB_NAME, | ||||
| 					TOPIC_NAME, | ||||
| 					orderId, | ||||
| 					singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS)).block(); | ||||
| 			log.info("Published data:" + orderId); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| ``` | ||||
| 
 | ||||
| Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: | ||||
|  | @ -843,14 +905,23 @@ import json | |||
| >>>>>>> 0e83af7a (Added pub sub documentation) | ||||
| ======= | ||||
| #dependencies   | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> fbadd23a (Modified based on the review comments - 1) | ||||
| ======= | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| import random | ||||
| from time import sleep     | ||||
| import requests | ||||
| import logging | ||||
| import json | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| from dapr.clients import DaprClient | ||||
| 
 | ||||
| #code | ||||
| logging.basicConfig(level = logging.INFO) | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
|      | ||||
| orderId = 100 | ||||
| <<<<<<< HEAD | ||||
|  | @ -869,6 +940,8 @@ logging.info('Published data: ' + str(orderId)) | |||
| <<<<<<< HEAD | ||||
|      | ||||
| ======= | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| while True: | ||||
|     sleep(random.randrange(50, 5000) / 1000) | ||||
|     orderId = random.randint(1, 1000) | ||||
|  | @ -883,7 +956,10 @@ while True: | |||
|             data_content_type='application/json', | ||||
|         ) | ||||
|     logging.info('Published data: ' + str(orderId)) | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| ``` | ||||
| 
 | ||||
| Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: | ||||
|  | @ -938,6 +1014,7 @@ var ( | |||
| ) | ||||
| 
 | ||||
| func main() { | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
|     orderId := 100 | ||||
|     client, err := dapr.NewClient() | ||||
|  | @ -975,6 +1052,18 @@ func main() { | |||
| 		defer client.Close() | ||||
| 		ctx := context.Background() | ||||
|     //Using Dapr SDK to publish a topic | ||||
| ======= | ||||
| 	for i := 0; i < 10; i++ { | ||||
| 		time.Sleep(5000) | ||||
| 		orderId := rand.Intn(1000-1) + 1 | ||||
| 		client, err := dapr.NewClient() | ||||
| 		if err != nil { | ||||
| 			panic(err) | ||||
| 		} | ||||
| 		defer client.Close() | ||||
| 		ctx := context.Background() | ||||
|     //Using Dapr SDK to publish a topic | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| 		if err := client.PublishEvent(ctx, PUBSUB_NAME, TOPIC_NAME, []byte(strconv.Itoa(orderId)));  | ||||
| 		err != nil { | ||||
| 			panic(err) | ||||
|  | @ -982,7 +1071,10 @@ func main() { | |||
| 
 | ||||
| 		log.Println("Published data: " + strconv.Itoa(orderId)) | ||||
| 	} | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| } | ||||
| ``` | ||||
| <<<<<<< HEAD | ||||
|  | @ -1010,10 +1102,13 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g | |||
| 
 | ||||
| ```javascript | ||||
| //dependencies | ||||
| import { DaprServer, DaprClient, CommunicationProtocolEnum } from 'dapr-client';  | ||||
| import { DaprServer, CommunicationProtocolEnum } from 'dapr-client';  | ||||
| 
 | ||||
| const daprHost = "127.0.0.1";  | ||||
| const serverHost = "127.0.0.1"; | ||||
| const serverPort = "6002";  | ||||
| 
 | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| var main = function() { | ||||
|  | @ -1073,10 +1168,29 @@ async function start(orderId) { | |||
|     //Using Dapr SDK to publish a topic | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
| ======= | ||||
| start().catch((e) => { | ||||
|     console.error(e); | ||||
|     process.exit(1); | ||||
| }); | ||||
| 
 | ||||
| async function start(orderId) { | ||||
|     const PUBSUB_NAME = "order_pub_sub" | ||||
| 	const TOPIC_NAME  = "orders" | ||||
|     const server = new DaprServer( | ||||
|         serverHost,  | ||||
|         serverPort,  | ||||
|         daprHost,  | ||||
|         process.env.DAPR_HTTP_PORT,  | ||||
|         CommunicationProtocolEnum.HTTP | ||||
|     ); | ||||
|     //Using Dapr SDK to publish a topic | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
|     await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => { | ||||
|         console.log(`Subscriber received: ${JSON.stringify(orderId)}`) | ||||
|     }); | ||||
|     await server.startServer(); | ||||
| <<<<<<< HEAD | ||||
| >>>>>>> f30a0acb (Added full code snippets of pub sub) | ||||
| ======= | ||||
|     await client.pubsub.publish(PUBSUB_NAME, TOPIC_NAME, orderId); | ||||
|  | @ -1102,6 +1216,9 @@ function sleep(ms) { | |||
| } | ||||
| 
 | ||||
| main(); | ||||
| ======= | ||||
| } | ||||
| >>>>>>> 8dfbce07 (Added full code snippets of pub sub) | ||||
| ``` | ||||
| 
 | ||||
| Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue