USER
I want you to act as a software development assistant, and help me create a library or wrapper for nats.ws, a WebSockets-based client for NATS messaging system. I may need guidance on how to structure the project, best practices for designing the API, and key functionalities to include. Additionally, provide examples of code snippets for connecting to a NATS server, subscribing to a topic, publishing a message, and handling reconnections. Expand on how to package and distribute this library, including setting up npm or other relevant package management configurations.
ASSISTANT
Sure, here's the detailed prompt:
---
I want you to act as a software development assistant, and help me create a library or wrapper for `nats.ws`, a WebSockets-based client for NATS messaging system. I may need guidance on how to structure the project, best practices for designing the API, and key functionalities to include.
1. **Project Structure**:
- How should I organize the project files and directories?
- What essential files should be included (e.g., `README.md`, `.gitignore`, license)?
2. **API Design**:
- How should I design the API to make it intuitive and easy to use?
- What objects, methods, and events should the API expose to users?
- How can I ensure the wrapper handles exceptions and errors gracefully?
3. **Key Functionalities**:
- Connecting to a NATS server
- Subscribing to a topic
- Publishing a message
- Handling reconnections and other edge cases
4. **Code Snippets**:
- Examples for connecting to a NATS server:
```javascript
const nats = require('nats.ws');
const nc = nats.connect({ servers: 'wss://demo.nats.io:443' });
nc.on('connect', () => {
console.log('Connected to NATS server');
});
nc.on('error', (err) => {
console.error('Connection error:', err);
});
```
- Example for subscribing to a topic:
```javascript
const subscription = nc.subscribe('updates', (msg) => {
console.log(`Received message: ${msg}`);
});
// To unsubscribe
subscription.unsubscribe();
```
- Example for publishing a message:
```javascript
nc.publish('updates', 'Hello, NATS!');
```
- Handling reconnections:
```javascript
nc.on('reconnect', () => {
console.log('Reconnected to NATS server');
});
nc.on('disconnect', () => {
console.log('Disconnected from NATS server');
});
```
5. **Packaging and Distribution**:
- How to set up `npm` for the library, including creating `package.json` and setting up scripts for building, testing, and publishing the package.
- Best practices for versioning, documenting, and maintaining the library.
- Example `package.json` content:
```json
{
"name": "nats-ws-wrapper",
"version": "1.0.0",
"description": "A library or wrapper for nats.ws, a WebSockets-based client for NATS messaging system",
"main": "index.js",
"scripts": {
"test": "jest",
"build": "webpack",
"prepublishOnly": "npm test && npm run build"
},
"repository": {
"type": "git",
"url": "https://github.com/yourusername/nats-ws-wrapper"
},
"keywords": [
"nats",
"websockets",
"messaging",
"library"
],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"nats.ws": "^1.0.0"
},
"devDependencies": {
"jest": "^26.6.0",
"webpack": "^5.4.0"
}
}
```
Please provide the detailed steps, insights, and any other recommendations that could help me successfully create, maintain, and distribute this library.