Skip to content

Commit 416416b

Browse files
authored
Migrate "easy event source" tutorial to Cloudevents javascript sdk v2 (#2518)
* Migrate to CloudEvents Javascript SDK v2 * Change to Javascript SDK version 2.0.1
1 parent bf5fef6 commit 416416b

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

docs/eventing/samples/writing-event-source-easy-way/README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ Code for this tutorial is available [here](https://github.com/knative/docs/tree/
1919
Create the project and add the dependencies:
2020
```bash
2121
npm init
22-
npm install cloudevents-sdk --save
22+
npm install cloudevents-sdk@2.0.1 --save
2323
```
2424

25+
Please note that because of a [bug](https://github.com/cloudevents/sdk-javascript/issues/191), you will
26+
need at least `2.0.1` version of the Javascript SDK.
27+
2528
## Making use of ContainerSource
2629

2730
`ContainerSource` and `SinkBinding` both work by injecting environment variables to an application.
@@ -33,34 +36,30 @@ The sink URL to post the events will be made available to the application via th
3336
```javascript
3437
// File - index.js
3538

36-
const v1 = require("cloudevents-sdk/v1");
39+
const { CloudEvent, HTTPEmitter } = require("cloudevents-sdk");
3740

3841
let sinkUrl = process.env['K_SINK'];
3942

4043
console.log("Sink URL is " + sinkUrl);
4144

42-
let config = {
43-
method: "POST",
45+
let emitter = new HTTPEmitter({
4446
url: sinkUrl
45-
};
46-
47-
// The binding instance
48-
let binding = new v1.BinaryHTTPEmitter(config);
47+
});
4948

5049
let eventIndex = 0;
5150
setInterval(function () {
5251
console.log("Emitting event #" + ++eventIndex);
5352

54-
// create the event
55-
let myevent = v1.event()
56-
.id('your-event-id')
57-
.type("your.event.source.type")
58-
.source("urn:event:from:your-api/resource/123")
59-
.dataContentType("application/json")
60-
.data({"hello": "World " + eventIndex});
53+
let myevent = new CloudEvent({
54+
source: "urn:event:from:my-api/resource/123",
55+
type: "your.event.source.type",
56+
id: "your-event-id",
57+
dataContentType: "application/json",
58+
data: {"hello": "World " + eventIndex},
59+
});
6160

6261
// Emit the event
63-
binding.emit(myevent)
62+
emitter.send(myevent)
6463
.then(response => {
6564
// Treat the response
6665
console.log("Event posted successfully");

docs/eventing/samples/writing-event-source-easy-way/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
const v1 = require("cloudevents-sdk/v1");
1+
const { CloudEvent, HTTPEmitter } = require("cloudevents-sdk");
22

33
let sinkUrl = process.env['K_SINK'];
44

55
console.log("Sink URL is " + sinkUrl);
66

7-
let config = {
8-
method: "POST",
7+
let emitter = new HTTPEmitter({
98
url: sinkUrl
10-
};
11-
12-
// The binding instance
13-
let binding = new v1.BinaryHTTPEmitter(config);
9+
});
1410

1511
let eventIndex = 0;
1612
setInterval(function () {
1713
console.log("Emitting event #" + ++eventIndex);
1814

19-
// create the event
20-
let myevent = v1.event()
21-
.id('your-event-id')
22-
.type("your.event.source.type")
23-
.source("urn:event:from:your-api/resource/123")
24-
.dataContentType("application/json")
25-
.data({"hello": "World " + eventIndex});
15+
let myevent = new CloudEvent({
16+
source: "urn:event:from:my-api/resource/123",
17+
type: "your.event.source.type",
18+
id: "your-event-id",
19+
dataContentType: "application/json",
20+
data: {"hello": "World " + eventIndex},
21+
});
2622

2723
// Emit the event
28-
binding.emit(myevent)
24+
emitter.send(myevent)
2925
.then(response => {
3026
// Treat the response
3127
console.log("Event posted successfully");

docs/eventing/samples/writing-event-source-easy-way/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12-
"cloudevents-sdk": "^1.0.0"
12+
"cloudevents-sdk": "^2.0.1"
1313
}
1414
}

0 commit comments

Comments
 (0)