-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsubscribe-event-ws.js
More file actions
67 lines (56 loc) · 1.81 KB
/
subscribe-event-ws.js
File metadata and controls
67 lines (56 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Part of a course on Hyperledger Fabric:
* http://ACloudFan.com
*
* Tested with Composer 0.20.5
*
* Pre-Requisites
* 1. Launch Fabric - Deploy Aircraft v8
* 2. Launch REST Server
*
* Uses the NPM library for websockets
* https://www.npmjs.com/package/websocket
*
* Demonstrates how applications can subscribe to the events
* published by the REST server
*
* 1. Setup the websocket library (npm install websocket --save) ... already done
* 2. Create a websocket client object
*
*/
var counter=0;
// #1 Need to use the websocket library
var WebSocketClient = require('websocket').client;
// #2 Create a WS Client
var client = new WebSocketClient();
// #3 Setup the WS connection failure listener
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
// #4 Setup the on connection listener
client.on('connect', (connection)=>{
console.log("Connected to REST-Server over WS protocol!!!");
// #5 Subscribe to messages received on WS
connection.on('message',(msg)=>{
var event = JSON.parse(msg.utf8Data);
// #6 Filter the events
switch(event.$class){
case 'org.acme.airline.flight.FlightCreated':
counter++;
console.log('Event#', counter);
processFlightCreatedEvent(event);
break;
default:
console.log("Ignored event: ", event.$class);
}
})
})
// #7 Call connect with URL to the REST Server
client.connect('ws://localhost:3000');
// #8 Gets called every time an event is receieved
function processFlightCreatedEvent(event){
console.log('Received event:')
// Pretty printing the received JSON string
console.log(JSON.stringify(event,null,4));
console.log();
}