Skip to content

Commit 62136b6

Browse files
committed
Add automatic detection to client.js between Node.js or browser
1 parent 2845852 commit 62136b6

File tree

7 files changed

+140
-91
lines changed

7 files changed

+140
-91
lines changed

.github/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ A simple JavaScript interface for communicating with CDP applications that inclu
77

88
## Installation
99

10+
### NPM Package (Recommended)
11+
12+
```bash
13+
npm install cdplogger-client
14+
```
15+
16+
### Development Setup
17+
1018
Clone the repository and install dependencies:
1119

1220
```bash
21+
git clone https://github.com/CDPTechnologies/JavascriptCDPLoggerClient.git
22+
cd JavascriptCDPLoggerClient
1323
npm install
1424
```
1525

DOCUMENTATION.md

Lines changed: 54 additions & 46 deletions
Large diffs are not rendered by default.

QUICKSTART.md

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,54 @@
11
# Quick Start Guide – CDPLogger Client
22

3-
This guide demonstrates how to get started with the CDPLogger Client in both **Node.js** and **Browser** environments. Note that the client module must be built differently for each target environment.
3+
This guide demonstrates how to get started with the CDPLogger Client in both **Node.js** and **Browser** environments. The client automatically detects the environment and works seamlessly in both.
44

55
## Documentation
66

77
For documentation on the JS logger client see [DOCUMENTATION.md](DOCUMENTATION.md)
88

99
## Overview
1010

11-
- **Node.js Version:**
12-
- Uses CommonJS modules (imported via `require()`).
13-
- Loads protobuf definitions from local files (e.g. from `./generated/containerPb.js`).
14-
- Requires a WebSocket polyfill (such as the `ws` package) since Node.js lacks a native WebSocket.
11+
The CDPLogger Client uses **automatic environment detection** to work in both Node.js and browser environments from a single source file:
1512

16-
- **Browser Version:**
17-
- Uses the browser's native WebSocket.
18-
- Obtains protobuf definitions from the global scope via `window.root` (set by including `containerPb.js`).
13+
- **Node.js:**
14+
- Automatically loads protobuf definitions using `require('./generated/containerPb.js')`
15+
- Uses the `ws` package for WebSocket functionality (installed as a dependency)
16+
- No manual setup required
17+
18+
- **Browser:**
19+
- Uses the browser's native WebSocket API
20+
- Expects protobuf definitions to be available at `window.root`
21+
- Requires including the containerPb.js script before the client
1922

2023
## Installation
2124

2225
### For Node.js
2326

24-
Install the CDPLogger Client and a WebSocket polyfill (for the Node.js version):
25-
2627
```bash
27-
npm install cdplogger-client ws
28+
npm install cdplogger-client
2829
```
2930

3031
### For Browser
3132

3233
Include the following scripts in your HTML:
3334
- `protobuf.min.js` – for the ProtoBuf runtime.
34-
- `containerPb.js` – which sets up `window.root` with your protobuf definitions.
35-
- The web version of `client.js` – which should include the web-specific modifications (see the "Adapting client.js for Web Support" section below).
36-
37-
## Adapting client.js for Web Support
38-
39-
To enable web support, ensure your `client.js` file includes these modifications:
40-
41-
1. **Use the Browser's Native WebSocket:**
42-
In the web version, remove or comment out any code that requires a WebSocket polyfill. For example:
43-
```js
44-
// const WebSocket = require('ws'); // Do not use this in the browser
45-
```
46-
47-
2. **Use Global Protobuf Definitions:**
48-
Instead of importing protobuf definitions via CommonJS, obtain them from the global scope:
35+
- `containerPb.js` – which sets up `window.root` with your protobuf definitions
36+
- `client.js` – the same file works in both Node.js and browser environments
37+
- Instead of importing protobuf definitions via CommonJS, obtain them from the global scope:
4938
```js
50-
// For Node.js, you might load:
51-
// const root = require('./generated/containerPb.js');
52-
// For Browser, use the global "root" defined by containerPb.js:
5339
const root = window.root;
5440
const Container = root.DBMessaging.Protobuf.Container;
5541
const CDPValueType = root.ICD.Protobuf.CDPValueType;
5642
const EventQuery = root.DBMessaging.Protobuf.EventQuery;
5743
```
58-
59-
*Make sure these modifications are only applied for the browser version of your client module.*
6044

6145
## Usage
6246

6347
### Node.js Example
6448

6549
```js
66-
// Import the client and set up the WebSocket polyfill
50+
// Import the client
6751
const cdplogger = require('cdplogger-client');
68-
global.WebSocket = require('ws');
6952

7053
// Create a client instance (endpoint can be "127.0.0.1:17000" or "ws://127.0.0.1:17000")
7154
const client = new cdplogger.Client('127.0.0.1:17000');
@@ -106,7 +89,7 @@ setTimeout(() => {
10689
}, 5000);
10790
```
10891

109-
*In the Node.js version, the module is imported using `require()`, and the WebSocket polyfill is provided by the `ws` package.*
92+
*The client automatically detects the Node.js environment and uses the `ws` package for WebSocket functionality.*
11093

11194
### Browser Example
11295

@@ -122,11 +105,17 @@ Create an HTML file that includes the necessary scripts. For example:
122105
<script src="protobuf.min.js"></script>
123106
<!-- Include containerPb.js to set up global "root" -->
124107
<script src="containerPb.js"></script>
125-
<!-- Include client.js (the web version with browser-specific modifications) -->
108+
<!-- Include client.js -->
126109
<script src="client.js"></script>
127110
</head>
128111
<body>
129112
<script>
113+
// Access protobuf definitions from global scope
114+
const root = window.root;
115+
const Container = root.DBMessaging.Protobuf.Container;
116+
const CDPValueType = root.ICD.Protobuf.CDPValueType;
117+
const EventQuery = root.DBMessaging.Protobuf.EventQuery;
118+
130119
// The client is now available globally as "cdplogger" (attached to window)
131120
// Use window.location.hostname to connect to the same host as the web page
132121
const client = new cdplogger.Client(window.location.hostname + ":17000");
@@ -169,7 +158,8 @@ Create an HTML file that includes the necessary scripts. For example:
169158
## Prerequisites
170159

171160
- **CDP Studio:** Ensure a CDP Studio application is running with an active **CDPLogger** (or LogServer) on a known WebSocket port (e.g., 17000).
172-
- **For Node.js:** Install the `ws` package as a WebSocket polyfill.
161+
- **For Node.js:** No additional setup required - WebSocket support is automatically configured.
162+
- **For Browser:** Ensure `containerPb.js` is available and sets up `window.root` with protobuf definitions.
173163

174164
## Learn More
175165

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CDP Logger Client
22

3-
A JavaScript client for reading historic data from systems created with the CDP Studio development platform.
3+
A JavaScript client for reading historic data from systems created with the CDP Studio development platform.
44
For more information about CDP Studio, see https://cdpstudio.com/.
55
For more information about CDP Logger, see https://cdpstudio.com/manual/cdp/cdplogger/cdplogger-index.html.
66

@@ -31,7 +31,6 @@ For detailed documentation, see [DOCUMENTATION.md](DOCUMENTATION.md).
3131

3232
```javascript
3333
const cdplogger = require('cdplogger-client');
34-
global.WebSocket = require('ws');
3534

3635
const client = new cdplogger.Client('127.0.0.1:17000');
3736

@@ -41,6 +40,36 @@ client.requestLoggedNodes().then(nodes => {
4140
});
4241
```
4342

43+
### Browser
44+
45+
```html
46+
<!DOCTYPE html>
47+
<html>
48+
<head>
49+
<script src="path/to/protobuf.min.js"></script>
50+
<script>
51+
// Make protobuf root available globally
52+
const root = window.root;
53+
const Container = root.DBMessaging.Protobuf.Container;
54+
const CDPValueType = root.ICD.Protobuf.CDPValueType;
55+
const EventQuery = root.DBMessaging.Protobuf.EventQuery;
56+
</script>
57+
<script src="path/to/generated/containerPb.js"></script>
58+
<script src="path/to/client.js"></script>
59+
</head>
60+
<body>
61+
<script>
62+
const client = new cdplogger.Client('ws://127.0.0.1:17000');
63+
64+
// List logged nodes
65+
client.requestLoggedNodes().then(nodes => {
66+
console.log("Available nodes:", nodes);
67+
});
68+
</script>
69+
</body>
70+
</html>
71+
```
72+
4473
## Contact
4574

4675
Email: support@cdptech.com

client.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
const WebSocket = require('ws');
2-
const root = require('./generated/containerPb.js');
1+
// Environment detection and dependency loading
2+
let root; // protobuf definitions
3+
let WS; // WebSocket constructor
4+
5+
if (typeof window === 'undefined') {
6+
// ---- Node / CommonJS ----
7+
root = require('./generated/containerPb.js');
8+
WS = global.WebSocket || require('ws');
9+
global.WebSocket = WS; // make sure anything else sees it
10+
} else {
11+
// ---- Browser ----
12+
root = window.root; // injected by <script src="containerPb.js">
13+
WS = window.WebSocket;
14+
}
15+
316
const Container = root.DBMessaging.Protobuf.Container;
417
const CDPValueType = root.ICD.Protobuf.CDPValueType;
518
const EventQuery = root.DBMessaging.Protobuf.EventQuery;
@@ -468,7 +481,7 @@ class Client {
468481
// --- Internal methods ---
469482

470483
_connect(url) {
471-
const ws = new WebSocket(url);
484+
const ws = new WS(url);
472485
ws._url = url;
473486
ws.binaryType = 'arraybuffer';
474487
ws.onopen = () => this._onOpen(ws);

examples/value.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// index.js
2-
// Example demonstration including an events query for a specific time range (UTC 9:40)
1+
// value.js
2+
// Example demonstration showing how to retrieve logged node data
33

4-
global.WebSocket = require('ws');
54
const cdplogger = require('../client');
65

76
// Print the node information (name, routing, and tags)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cdplogger-client",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A JavaScript client for interacting with CDP Logger or LogServer via WebSocket",
55
"main": "client.js",
66
"files": [

0 commit comments

Comments
 (0)