-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathexample.js
More file actions
111 lines (100 loc) · 3.78 KB
/
example.js
File metadata and controls
111 lines (100 loc) · 3.78 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
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
"use strict";
var DDPClient = require("../lib/ddp-client");
var ddpclient = new DDPClient({
// All properties optional, defaults shown
host : "localhost",
port : 3000,
ssl : false,
autoReconnect : true,
autoReconnectTimer : 500,
maintainCollections : true,
ddpVersion : "1", // ["1", "pre2", "pre1"] available,
// uses the sockJs protocol to create the connection
// this still uses websockets, but allows to get the benefits
// from projects like meteorhacks:cluster
// (load balancing and service discovery)
// do not use `path` option when you are using useSockJs
useSockJs: true,
// Use a full url instead of a set of `host`, `port` and `ssl`
// do not set `useSockJs` option if `url` is used
url: 'wss://example.com/websocket'
});
/*
* Connect to the Meteor Server
*/
ddpclient.connect(function(error, wasReconnect) {
// If autoReconnect is true, this callback will be invoked each time
// a server connection is re-established
if (error) {
console.log("DDP connection error!");
return;
}
if (wasReconnect) {
console.log("Reestablishment of a connection.");
}
console.log("connected!");
setTimeout(function () {
/*
* Call a Meteor Method
*/
ddpclient.send(
"deletePosts", // name of Meteor Method being called
["foo", "bar"], // parameters to send to Meteor Method
function (err, result) { // callback which returns the method call results
console.log("called function, result: " + result);
},
function () { // callback which fires when server has finished
console.log("updated"); // sending any updated documents as a result of
console.log(ddpclient.collections.posts); // calling this method
}
);
}, 3000);
/*
* Call a Meteor Method while passing in a random seed.
* Added in DDP pre2, the random seed will be used on the server to generate
* repeatable IDs. This allows the same id to be generated on the client and server
*/
var Random = require("ddp-random"),
random = Random.createWithSeeds("randomSeed"); // seed an id generator
ddpclient.sendWithRandomSeed(
"createPost", // name of Meteor Method being called
[{ _id : random.id(), // generate the id on the client
body : "asdf" }],
"randomSeed", // pass the same seed to the server
function (err, result) { // callback which returns the method call results
console.log("called function, result: " + result);
},
function () { // callback which fires when server has finished
console.log("updated"); // sending any updated documents as a result of
console.log(ddpclient.collections.posts); // calling this method
}
);
/*
* Subscribe to a Meteor Collection
*/
ddpclient.subscribe(
"posts", // name of Meteor Publish function to subscribe to
[], // any parameters used by the Publish function
function () { // callback when the subscription is complete
console.log("posts complete:");
console.log(ddpclient.collections.posts);
}
);
/*
* Observe a collection.
*/
var observer = ddpclient.observe("posts");
observer.added = function(id) {
console.log("[ADDED] to " + observer.name + ": " + id);
};
observer.changed = function(id, oldFields, clearedFields) {
console.log("[CHANGED] in " + observer.name + ": " + id);
console.log("[CHANGED] old field values: ", oldFields);
console.log("[CHANGED] cleared fields: ", clearedFields);
};
observer.removed = function(id, oldValue) {
console.log("[REMOVED] in " + observer.name + ": " + id);
console.log("[REMOVED] previous value: ", oldValue);
};
setTimeout(function() { observer.stop(); }, 6000);
});