-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrx-rpc-server.html
117 lines (99 loc) · 4.43 KB
/
rx-rpc-server.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple RPC Server using RxStomp and RxJS</title>
<link type="text/css" rel="stylesheet" href="../../assets/style.css">
</head>
<body>
<div id="wrapper">
<h1>Simple RPC Server</h1>
<ul>
<li>You will need a STOMP broker running. The defaults will work for fresh RabbitMQ on local machine.</li>
<li>Adjust the <a href="https://stomp-js.github.io/api-docs/latest/classes/RxStompConfig.html">configuration</a>
as per your STOMP broker.
</li>
<li>
This example uses <a href="https://www.rabbitmq.com/stomp.html#d">queue destination of RabbitMQ</a>. The queue must be created before running the sample.
The ruby server creates the queue if it is not there.
</li>
<li>
The server endpoint will wait for a random period (max 1000ms) before responding.
</li>
<li>Request and responses are logged into Console.</li>
<li>
See guide at
<a href="https://stomp-js.github.io/guide/rx-stomp/ng2-stompjs/2018/10/12/remote-procedure-call.html">
https://stomp-js.github.io/guide/rx-stomp/ng2-stompjs/2018/10/12/remote-procedure-call.html</a>
</li>
</ul>
</div>
<!-- It is used for DOM manipulation, not mandatory to use stompjs -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<!-- Include from CDN for better performance, alternatively you can locally copy as well -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/bundles/rxjs.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@stomp/[email protected]/bundles/stomp.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@stomp/[email protected]/bundles/rx-stomp.umd.min.js"></script>
<script type="application/javascript">
// Helper function to generate random integers
function randomInt(max) {
return Math.floor(Math.random() * max);
}
// Destination is RabbitMQ specific, change as per your environment
const rpcEndPoint = '/amq/queue/integer-addition';
// We will open two separate connections
// This block will be used as server - in typical applications the RPC server will be somewhere in back
// RPC server may be implemented in any language
$(function () {
let rxStomp;
const stompConfig = {
// Typically login, passcode and vhost
// Adjust these for your broker
connectHeaders: {
login: "guest",
passcode: "guest"
},
// Broker URL, should start with ws:// or wss:// - adjust for your broker setup
brokerURL: "ws://localhost:15674/ws",
// Keep it off for production, it can be quit verbose
// Skip this key to disable
// debug: function (str) {
// console.log('RPC Server: ' + str);
// },
// If disconnected, it will retry after 200ms
reconnectDelay: 200,
};
// Create an instance. The first RxStomp is the UMD module name and other is the class name
rxStomp = new RxStomp.RxStomp();
// You can set additional configuration here
rxStomp.configure(stompConfig);
// Attempt to connect
rxStomp.activate();
// We will implement an endpoint
// This endpoint will wait for random period before responding to simulate real RPC servers
rxStomp.watch(rpcEndPoint).subscribe(function (request) {
console.log("RPC Server: Request: " + request.body);
// The response needs to be sent back here, it can safely be inlined
const replyTo = request.headers['reply-to'];
// Same correlation id needs to be sent back as message header, it can safely be inlined
const correlationId = request.headers['correlation-id'];
// simulate a random delay while computing
setTimeout(function () {
// Process the request, compute the response
const operands = JSON.parse(request.body);
const result = {result: Number.parseInt(operands.x) + Number.parseInt(operands.y)};
// Completed processing
const responseBody = JSON.stringify(result);
console.log("RPC Server: Response: " + responseBody + " for " + request.body);
// Send the response back to destination `replyTo` with `correlation-id` header
rxStomp.publish({
destination: replyTo,
body: responseBody,
headers: {'correlation-id': correlationId}
});
}, randomInt(10000));
});
});
</script>
</body>
</html>