1 00:00:00,790 --> 00:00:04,570 We have connected to our Net Server successfully so we're now going to attempt to publish some data 2 00:00:04,570 --> 00:00:05,340 to it. 3 00:00:05,350 --> 00:00:09,550 Let me first tell you a quick diagram just to introduce a couple of different pieces of terminology 4 00:00:09,640 --> 00:00:11,010 on how we're going to publish data. 5 00:00:12,000 --> 00:00:12,250 OK. 6 00:00:12,290 --> 00:00:16,680 So in this diagram on the left hand side we've got our publisher and a listener. 7 00:00:16,680 --> 00:00:20,130 We have not yet put the listener together but I want to give you a brief description of what we're going 8 00:00:20,130 --> 00:00:21,900 to eventually do inside there. 9 00:00:21,990 --> 00:00:24,540 Then on the right hand side is our Nat string server. 10 00:00:24,750 --> 00:00:27,320 And inside there is some list of channels. 11 00:00:27,390 --> 00:00:33,020 Remember in the Nats world we always publish information to a very specific channel. 12 00:00:33,060 --> 00:00:36,920 Here's how we actually share information around inside of our publisher. 13 00:00:36,930 --> 00:00:42,030 We're gonna come up with some piece of data that we want to share with everything else that's listening 14 00:00:42,030 --> 00:00:43,500 to the stats server. 15 00:00:43,590 --> 00:00:46,670 So we're going to create some object or something like that. 16 00:00:46,760 --> 00:00:52,230 We're then also gonna come up with a subject a subject is the name of the channel that we want to share 17 00:00:52,230 --> 00:00:57,900 this information with you and I we're going to imagine that we're trying to share some data about a 18 00:00:57,900 --> 00:01:00,660 ticket that was just created inside of our application. 19 00:01:00,660 --> 00:01:02,770 So our data is going to be essentially a ticket. 20 00:01:02,790 --> 00:01:09,880 It will be something it has an IP a title and a price are subject or the name of the channel that we 21 00:01:09,880 --> 00:01:11,700 want to share this information with. 22 00:01:11,740 --> 00:01:16,450 We're gonna give it a name that kind of describes the type of data that we're trying to share. 23 00:01:16,450 --> 00:01:21,190 So in our case we are trying to share some data related to the creation of a ticket. 24 00:01:21,280 --> 00:01:26,660 The name of our channel will be ticket colon created. 25 00:01:26,770 --> 00:01:32,860 We're gonna take this data and these subjects and pass it in to the stand client the stand client is 26 00:01:32,860 --> 00:01:37,760 then going to reach over to the Nat string server when it reaches over. 27 00:01:37,840 --> 00:01:44,440 It's going to say hey we want to share some information specifically this data over the channel or the 28 00:01:44,500 --> 00:01:52,630 subject name of ticket created the Nat string server is then going to add ticket colon created to its 29 00:01:52,660 --> 00:01:54,710 overall list of channels. 30 00:01:54,710 --> 00:02:01,900 It'll then take our data and broadcast it out of that channel to anyone who's listening now at present 31 00:02:01,900 --> 00:02:03,190 inside our application. 32 00:02:03,190 --> 00:02:06,430 We don't really have any implementation for the listener at all. 33 00:02:06,460 --> 00:02:13,000 So if we tried to share data or published some data on this channel right now that server will say OK 34 00:02:13,090 --> 00:02:16,660 I will try to do this but just so you know no one's really listening. 35 00:02:16,780 --> 00:02:20,260 So we can always attempt to publish data and if no one's listening. 36 00:02:20,260 --> 00:02:21,670 Well that's pretty much it. 37 00:02:21,730 --> 00:02:26,530 Our data is just going to be emitted into the void and no one's really going to hear from us about whatever 38 00:02:26,530 --> 00:02:32,880 just occurred so at some point time we will create this listener when we make the listener. 39 00:02:32,970 --> 00:02:35,680 We're going to have a subject that we want to listen to. 40 00:02:35,760 --> 00:02:40,140 So in our case the subject we want to listen to are the channel we want to listen to will be ticket 41 00:02:40,140 --> 00:02:47,810 created so we'll take ticket created and pass it also into the stand client that's going to tell this 42 00:02:47,810 --> 00:02:55,140 damn client to tell the NAT server that anytime some information is published from this channel we want 43 00:02:55,140 --> 00:03:00,680 to receive a copy of that information and we'll receive that information in something called the subscription. 44 00:03:00,760 --> 00:03:05,430 So a subscription is what is going to actually listen for some information and tell us whenever some 45 00:03:05,430 --> 00:03:08,110 information is received again. 46 00:03:08,140 --> 00:03:09,520 So it's kind of a high level overview. 47 00:03:09,520 --> 00:03:13,490 And mostly I just want to show you some different important pieces of terminology. 48 00:03:13,570 --> 00:03:19,060 So the subject is the name of the channel we want to publish information to the channel is something 49 00:03:19,060 --> 00:03:24,350 that we listen to the subscription is what's going to actually listen to the channel and eventually 50 00:03:24,350 --> 00:03:30,180 it receives some data let's now go back over to our publisher T.S. file and we're going to try to publish 51 00:03:30,240 --> 00:03:36,510 an event back inside a publisher T.S. right after our console log. 52 00:03:36,530 --> 00:03:38,510 I'm going to write out the data object. 53 00:03:38,510 --> 00:03:41,590 So this will be the information we want to share. 54 00:03:41,630 --> 00:03:45,870 We're going to again kind of pretend that we're sharing information about a ticket being created. 55 00:03:45,890 --> 00:03:52,880 So we'll give this thing some I.D. a title of concerts and maybe a price of 20. 56 00:03:52,880 --> 00:03:59,180 Now there is a little gotcha around that's in the gotcha is that we can only essentially share strings 57 00:03:59,180 --> 00:04:00,900 or essentially raw data. 58 00:04:01,730 --> 00:04:04,530 What we have right here is an object that we want to share. 59 00:04:04,730 --> 00:04:11,500 So we can not share directly a plain javascript object in order to share this or send over to that string 60 00:04:11,500 --> 00:04:11,930 server. 61 00:04:11,950 --> 00:04:15,820 We first have to convert it into Jason which is a plain string. 62 00:04:15,820 --> 00:04:20,380 So essentially all of our data before we send over to that streaming server we just have to convert 63 00:04:20,380 --> 00:04:22,180 it into Jason. 64 00:04:22,180 --> 00:04:33,330 So I got to wrap that with Jason trying to find like some then after that we'll call the Stan publish 65 00:04:33,390 --> 00:04:37,940 function the publish function is how we actually publish some data when we call publish. 66 00:04:37,950 --> 00:04:42,780 We're going to pass in the name of the channel or the subject we want to publish this information too. 67 00:04:42,870 --> 00:04:46,110 And we'll also pass in the data we want to share as well. 68 00:04:46,110 --> 00:04:50,280 So we'll call Stan dot publish the first argument is gonna be the subject name. 69 00:04:50,280 --> 00:04:55,530 So in our case that is ticket calling created the second argument is the data we want to share. 70 00:04:55,650 --> 00:04:57,810 There's then an optional third argument. 71 00:04:57,810 --> 00:04:59,240 So we do not have to do this. 72 00:04:59,250 --> 00:05:00,880 We can't if we want to. 73 00:05:00,900 --> 00:05:03,900 The third argument is a callback function. 74 00:05:03,900 --> 00:05:08,140 This function is going to be invoked after we publish this data. 75 00:05:08,190 --> 00:05:13,510 So inside of here we might just put in a simple console log and say event published 76 00:05:16,900 --> 00:05:19,470 let's now save this file just so you know the. 77 00:05:19,620 --> 00:05:24,760 Yes no Dev program that we're using inside of our package based on script is going to automatically 78 00:05:24,790 --> 00:05:27,680 rerun the publisher test file as soon as we save it. 79 00:05:27,700 --> 00:05:33,430 So as soon as we save this we should see that we reconnect to our Nats cluster or Nat server and then 80 00:05:33,430 --> 00:05:35,540 attempt to publish some information. 81 00:05:35,600 --> 00:05:38,830 So going to say this go backward my terminal. 82 00:05:38,830 --> 00:05:44,060 And sure enough I see I was sure connected to Nats and then even published. 83 00:05:44,100 --> 00:05:44,360 All right. 84 00:05:44,430 --> 00:05:47,520 Well that's the basics of how we publish events. 85 00:05:47,590 --> 00:05:52,620 They're just you know in the world of Nats rather than referring to this data that we send along as 86 00:05:52,710 --> 00:05:54,020 a event or something like that. 87 00:05:54,030 --> 00:05:56,450 It is more commonly referred to as a message. 88 00:05:56,520 --> 00:06:00,840 So you will see a lot of stuff in the Nats documentation referring to this data where this information 89 00:06:00,840 --> 00:06:05,760 was sent out as a message whereas in this course referring to it as an event because that's really what 90 00:06:05,760 --> 00:06:10,320 it is to us it's an event it's something that occurred inside of our app that we want to share with 91 00:06:10,350 --> 00:06:12,600 other services. 92 00:06:12,660 --> 00:06:16,860 Now we've got our publisher put together with a very basic implementation. 93 00:06:16,860 --> 00:06:20,060 We're going to take a pause and then tackle our listener in the next video.