1 | ## Diffusion JavaScript API
|
2 |
|
3 | The Diffusion JavaScript API allows interaction with a Diffusion server from both
|
4 | the browser and Node.js.
|
5 |
|
6 | Clients use a WebSocket or HTTP connection to send and receive data, as well as perform other functions such as
|
7 | adding, removing or updating topics. For a full list of capabilities in the JavaScript API, see the
|
8 | [Contents](https://download.pushtechnology.com/docs/6.4.5/js/index.html#section-contents) section
|
9 | below.
|
10 |
|
11 | ### Installing
|
12 | Install the client using ```npm install diffusion --save```
|
13 |
|
14 | Pre-bundled browser artifacts are included in ```node_modules/diffusion/dist```
|
15 |
|
16 | To import Diffusion use either `require` or ES6 style syntax
|
17 |
|
18 | **require**
|
19 | ```javascript
|
20 | var diffusion = require('diffusion');
|
21 | ```
|
22 |
|
23 | **ES6**
|
24 | ```javascript
|
25 | import * as diffusion from 'diffusion';
|
26 | ```
|
27 |
|
28 | ### TypeScript
|
29 | The Diffusion package includes TypeScript bindings, specified in ```node_modules/diffusion/typescript```
|
30 |
|
31 | ### The diffusion object
|
32 |
|
33 | The documentation will frequently refer to the `diffusion` object. Technically,
|
34 | this is the module object that is obtained when importing the Diffusion library
|
35 | using one of the methods described above. The diffusion object exposes a number
|
36 | of global constants and functions.
|
37 |
|
38 | #### Constants
|
39 |
|
40 | [`version: string;`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#version)<br>
|
41 | The version of this client library in the form major.minor.patch
|
42 |
|
43 | [`build: string`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#build)<br>
|
44 | The build version of this client library
|
45 |
|
46 | #### Functions
|
47 |
|
48 | [`function log(level: LogLevel | keyof typeof LogLevel)`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#log)<br>
|
49 | Set the level of logging used by Diffusion.
|
50 |
|
51 |
|
52 | [`function connect(options: Options): Result<Session>`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#connect)<br>
|
53 | Connect to a specified Diffusion server.
|
54 |
|
55 | [`function escape(s: string)`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#escape)<br>
|
56 | Escapes special characters in a string that is to be used within a topic
|
57 | property or a session filter.
|
58 |
|
59 | [`function stringToRoles(s: string)`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#stringtoroles)<br>
|
60 | Utility method which converts a string of the format required by the
|
61 | [$Roles](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/propertykeys.html#roles)
|
62 | session property into a mutable set of strings.
|
63 |
|
64 | [`rolesToString(roles: Set<string> | string[])`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#rolestostring)<br>
|
65 | Utility method which converts a set of authorization roles to the string format required by the
|
66 | [$Roles](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/propertykeys.html#roles)
|
67 | session property.
|
68 |
|
69 | [`function updateConstraints(): UpdateConstraintFactory`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#updateconstraints)<br>
|
70 | Returns an update constraint factory.
|
71 |
|
72 | #### Namespaces
|
73 |
|
74 | [`const datatypes`](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/datatypes.html)<br>
|
75 | Access to the *datatypes* namespace
|
76 |
|
77 | [`const locks`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#sessionlockoptions)<br>
|
78 | Access to the *locks* namespace
|
79 |
|
80 | [`const selectors `](https://download.pushtechnology.com/docs/6.4.5/js/classes/topicselectors.html)<br>
|
81 | Access to the *selectors* namespace
|
82 |
|
83 | [`const topics`](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topicsnamespace.html)<br>
|
84 | Access to the *topics* namespace
|
85 |
|
86 | [`const topicUpdate`](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topicupdatenamespace.html)<br>
|
87 | Access to the *topicUpdate* namespace
|
88 |
|
89 | [`const errors`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#errorreason)<br>
|
90 | The [ErrorReason](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#errorreason) enum
|
91 |
|
92 | [`const clients`](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#clientcontroloptions)<br>
|
93 | Access to
|
94 | [PropertyKeys](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#clientcontroloptions.propertykeys)
|
95 | and other client control options
|
96 |
|
97 | ### Quick start
|
98 |
|
99 | A client [Session](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html)
|
100 | maintains a connection to the server. To create a session, simply do
|
101 |
|
102 | ```javascript
|
103 | diffusion.connect('diffusion.example.com');
|
104 | ```
|
105 |
|
106 | It is also possible to
|
107 | [connect](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#connect) with a map of
|
108 | [options](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/options.html)
|
109 |
|
110 | ```javascript
|
111 | diffusion.connect({
|
112 | host : 'diffusion.example.com',
|
113 | port : 8080,
|
114 | secure : false,
|
115 | principal : 'admin',
|
116 | credentials : 'password'
|
117 | });
|
118 | ```
|
119 |
|
120 | Connecting returns a [Promise](https://promisesaplus.com/) - this will succeed if the session could be connected, or
|
121 | fail if not.
|
122 |
|
123 | ```javascript
|
124 | diffusion.connect('diffusion.example.com').then(function(session) {
|
125 | // Connected!
|
126 | }, function(error) {
|
127 | // Failed to connect :(
|
128 | });
|
129 | ```
|
130 |
|
131 | Sessions emit events to indicate their status such as when they are
|
132 | [disconnected](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#event-disconnect)
|
133 | or [closed](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#event-close).
|
134 | These events are easy to listen to:
|
135 |
|
136 | ```javascript
|
137 | session.on('disconnect', function(reason) {
|
138 | // Lost connection to the server!
|
139 | });
|
140 |
|
141 | session.on('close', function() {
|
142 | // Session is closed!
|
143 | });
|
144 | ```
|
145 |
|
146 | Once a session is closed, it can never be re-opened.
|
147 |
|
148 |
|
149 | #### Subscriptions
|
150 |
|
151 | Data in Diffusion is distributed via *topics*. A topic has state, which can be updated. The state can be simple - such
|
152 | as a string or an integer - or more complex - such as a JSON document. Each topic has a unique path and is addressed
|
153 | through a [topic
|
154 | selector](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topicselector.html).
|
155 |
|
156 | The way that a session receives data is by
|
157 | [subscribing](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#select).
|
158 | Subscriptions allow the session to select one or more topics to receive values from. A session may subscribe to many
|
159 | topics, as well as subscribe to the same topic multiple times.
|
160 |
|
161 | ```javascript
|
162 | session.select('topic/foo')
|
163 | ```
|
164 |
|
165 | To attach listeners for received values, a
|
166 | [ValueStream](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/valuestream.html) is used.
|
167 | The stream that is returned will emit
|
168 | [value](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/valuestream.html#event-value)
|
169 | events when the value of the selected topic changes.
|
170 |
|
171 | ```javascript
|
172 | session.addStream('topic/foo', diffusion.datatypes.json()).on('value', function(topic, specification, newValue, oldValue) {
|
173 | // Do something with the value
|
174 | var value = newValue.get();
|
175 | });
|
176 | ```
|
177 | It is possible to register any number of streams to a subscription's events. They will each be called when a new
|
178 | value is received.
|
179 |
|
180 | ### <a name="section-contents"></a> Contents
|
181 |
|
182 | The JavaScript client provides namespaces, classes, and methods that support the following capabilities:
|
183 |
|
184 | #### Basics
|
185 |
|
186 | * Connect the JavaScript client to Diffusion or Diffusion Cloud by using the
|
187 | [diffusion.connect](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#connect) method.
|
188 |
|
189 | * To change the security principal that the client is connected with, use the
|
190 | [changePrincipal](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#changeprincipal) method.
|
191 |
|
192 | * The client can log out information by using the
|
193 | [diffusion.log](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#log) method.
|
194 |
|
195 | * The client can check its connectivity and roundtrip time to the server by using the
|
196 | [pingServer](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#pingserver) method.
|
197 |
|
198 |
|
199 | #### Receive data from topics
|
200 |
|
201 | * Subscribe to topics.
|
202 | Use the
|
203 | [session.select](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#select)
|
204 | method to subscribe to a topic. The updates to a topic can be interacted with by registering a
|
205 | [ValueStream](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/valuestream.html) and
|
206 | a provided datatype to start receiving the values of that datatype.
|
207 |
|
208 | * Fetch data from topics.
|
209 | Use the [fetch](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/session.html#fetch)
|
210 | method to make a fetch request and get a
|
211 | [FetchStream](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/fetchstream.html)
|
212 | object that you can use to receive fetched values.
|
213 |
|
214 |
|
215 | #### Create and manage topics
|
216 |
|
217 | Creating and managing topics is handled through the [topics](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topiccontrol.html) feature.
|
218 |
|
219 | * Add a topic.
|
220 | Use the [add](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topiccontrol.html#add)
|
221 | method to add a topic. You can create a topic by explicitly defining the topic type, or by providing a
|
222 | [TopicSpecification](https://download.pushtechnology.com/docs/6.4.5/js/classes/topicspecification.html)
|
223 | with optional properties.
|
224 |
|
225 | * Handle missing topics.
|
226 | Use the
|
227 | [addMissingTopicHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topiccontrol.html#addmissingtopichandler)
|
228 | method to register a
|
229 | [MissingTopicHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/missingtopichandler.html).
|
230 | This handler receives a
|
231 | [MissingTopicNotification](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/missingtopicnotification.html)
|
232 | when a client session subscribes to a topic that does not currently exist. The notified client can then choose to
|
233 | create that topic if appropriate.
|
234 |
|
235 | * Remove topics.
|
236 | Use the
|
237 | [remove](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/topiccontrol.html#remove)
|
238 | method to remove topics. You can also mark topics to be removed automatically with the TopicSpecification property
|
239 | [REMOVAL](https://download.pushtechnology.com/docs/6.4.5/js/classes/topicspecification.html#removal).
|
240 |
|
241 |
|
242 | #### Update topics
|
243 |
|
244 | Updating topics is handled through the [topics](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#topic-update-feature) feature.
|
245 |
|
246 | * Update a topic using the [set](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#topicupdate.set) method.
|
247 |
|
248 | * Create an update stream for creating and updating a specific topic using the
|
249 | [createUpdateStream](https://download.pushtechnology.com/docs/6.4.5/js/globals.html#topicupdate.createupdatestream)
|
250 |
|
251 | * Update topics by using the classes in the
|
252 | [diffusion.datatypes](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/datatypes.html)
|
253 | namespace to create update values.
|
254 |
|
255 |
|
256 | #### Manage other clients
|
257 |
|
258 | Updating topics is handled through the [clients](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/clientcontrol.html) feature.
|
259 |
|
260 | * Subscribe other client sessions to topics.
|
261 | Use the
|
262 | [subscribe](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/clientcontrol.html#subscribe)
|
263 | and
|
264 | [unsubscribe](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/clientcontrol.html#unsubscribe)
|
265 | methods to manage another client session's subscriptions.
|
266 |
|
267 | * Receive notifications about other sessions and their properties.
|
268 | Use the
|
269 | [setSessionPropertiesListener](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/clientcontrol.html#setsessionpropertieslistener)
|
270 | method to register a
|
271 | [SessionPropertiesListener](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/sessionpropertieslistener.html)
|
272 | to receive notifications for session events or when a session changes properties.
|
273 |
|
274 | * Query the session properties of a specific session.
|
275 | Use the
|
276 | [getSessionProperties](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/clientcontrol.html#getsessionproperties)
|
277 | method. Specify which sets of properties to receive using
|
278 | [PropertyKeys](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/propertykeys.html).
|
279 |
|
280 |
|
281 | #### Send messages
|
282 |
|
283 | Sending and receiving messages is handled through the [messages](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/messages.html) feature.
|
284 |
|
285 | * Send a message.
|
286 | Use the
|
287 | [send](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/messages.html#sendrequest)
|
288 | method to either send a message to a specific client session or send a message to a path.
|
289 |
|
290 | * Receive messages sent to this client session.
|
291 | Use the
|
292 | [setRequestStream](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/messages.html#setrequeststream)
|
293 | method to receive messages sent to this client session through a
|
294 | [RequestStream](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/requeststream.html).
|
295 |
|
296 | * Receive messages sent to a path.
|
297 | Use the
|
298 | [addRequestHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/messages.html#addrequesthandler)
|
299 | method to register a
|
300 | [RequestHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/requesthandler.html)
|
301 | that receives messages sent to a path.
|
302 |
|
303 |
|
304 | #### Authenticating clients
|
305 |
|
306 | Authenticating clients and modifying the authentication store is handled through the [security](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html) feature.
|
307 |
|
308 | * Register an authentication handler to authenticate other client sessions.
|
309 | Implement an
|
310 | [AuthenticationHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/authenticationhandler.html)
|
311 | and use the
|
312 | [setAuthenticationHandler](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#setauthenticationhandler)
|
313 | method to register it with the server.
|
314 |
|
315 | The server also uses information stored in the system authentication store to authenticate connecting clients.
|
316 |
|
317 | * Get the system authentication store information with the
|
318 | [getSystemAuthenticationConfiguration](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#getsystemauthenticationconfiguration) method.
|
319 |
|
320 | * Update the system authentication store.
|
321 | Use the
|
322 | [authenticationScriptBuilder](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#authenticationscriptbuilder)
|
323 | method to get a
|
324 | [SystemAuthenticationScriptBuilder](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/systemauthenticationscriptbuilder.html).
|
325 | The builder can be used to create a script. Pass the script to the
|
326 | [updateAuthenticationStore](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#updateauthenticationstore)
|
327 | method to update the system authentication store.
|
328 |
|
329 |
|
330 | #### Updating security roles
|
331 |
|
332 | The [security](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html) feature also allows querying and updating a session's security roles.
|
333 |
|
334 | * Get the security store information [getSecurityConfiguration](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#getsecurityconfiguration) method.
|
335 |
|
336 | * Update the security store.
|
337 | Use the
|
338 | [securityScriptBuilder](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#securityscriptbuilder)
|
339 | method to get a
|
340 | [SecurityScriptBuilder](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/securityscriptbuilder.html).
|
341 | The builder can be used to create a script. Pass the script to the
|
342 | [updateSecurityStore](https://download.pushtechnology.com/docs/6.4.5/js/interfaces/security.html#updatesecuritystore)
|
343 | method to update the system authentication store.
|