dpsn-client-nodejs is an SDK for creating, accessing data streams on DPSN infrastructure. It allows you to connect to a DPSN broker, publish messages to topics, and subscribe to topics to receive messages.
For more information, visit:
npm install dpsn-client- DPSN URL: Your DPSN broker URL (e.g., betanet.dpsn.org)
- DPSN Access Token: Your private key for authentication
import {DpsnClient} from 'dpsn-client';// With SSL (mqtts://)
const dpsnClient = new DpsnClient('<dpsn_url>', '<dpsn_access_token>', true);
// Without SSL (mqtt://)
const dpsnClient = new DpsnClient('<dpsn_url>', '<dpsn_access_token>', false);
// Default behavior (client defaults to mqtts://)
const dpsnClient = new DpsnClient('<dpsn_url>', '<dpsn_access_token>');Topics in DPSN are data distribution channels that enable secure data streams. They function as:
- Data streams: Publishers push data to topics, and subscribers receive data from topics
- Authenticated channels: Authentication is provided through cryptographic signatures
Note: Set up these event handlers before calling
init()to properly handle connection events
dpsnClient.on('connect', (res) => {
console.log(res);
})
dpsnClient.on('publish', (res) => {
console.log(res);
})
dpsnClient.on('subscription', (res) => {
console.log("STARTED SUBSCRIPTION:", res)
})
dpsnClient.on('disconnect', (res) => {
console.log(res);
})
dpsnClient.on('error', (err) => {
console.log("OPERATION FAILED: ", err);
})await dpsnClient.init()await dpsnClient.publish('<topic>', <data>);To subscribe to a topic and handle incoming messages, use the subscribe method:
await dpsnClient.subscribe('<topic>', (message) => {
console.log("Received message:", message);
});To unsubscribe from a topic when you no longer want to receive messages, use the unsubscribe method:
const result = await dpsnClient.unsubscribe('<topic>');
console.log(`Successfully unsubscribed from ${result.topic}: ${result.message}`);This method returns a response object containing:
topic: The topic you unsubscribed frommessage: A confirmation message ("unsubscribed")
For properly terminating the connection when needed
await dpsnClient.disconnect();constructor(dpsnUrl: string, dpsn_accestoken: string, ssl?: boolean)Parameters:
dpsnUrl- The DPSN broker URLdpsn_accestoken- Your private key for authenticationssl- Optional. Iftrue, forcesmqtts://. Iffalse, forcesmqtt://. If not provided, uses the protocol from the URL.
init(options?: InitOptions): Promise<MqttClient>- Initialize the DPSN client and connect to the brokeron(event: DpsnEventType, callback: Function): this- Register event handlers for various events (connect, subscription, publish, disconnect, error)onConnect(callback: (message: string) => void): void- Deprecated: Register a callback for connection events (useon('connect', callback)instead)onError(callback: (error: Error | DPSNError) => void): void- Deprecated: Register a callback for error events (useon('error', callback)instead)publish(topic: string, message: any, options?: Partial<mqtt.IClientPublishOptions>): Promise<void>- Publish a message to a topicsubscribe(topic: string, callback: (message: any) => void, options?: mqtt.IClientSubscribeOptions): Promise<void>- Subscribe to a topicunsubscribe(topic: string): Promise<{topic: string, message: string}>- Unsubscribe from a topic and stop receiving messagesdisconnect(): Promise<void>- Disconnect from the MQTT broker and clean up resources
interface InitOptions {
connectTimeout?: number;
retryOptions?: {
maxRetries?: number;
initialDelay?: number;
maxDelay?: number;
exponentialBackoff?: boolean;
};
}type DpsnEventType = 'connect' | 'subscription' | 'publish' | 'disconnect' | 'error';type DpsnEventData = {
connect: string;
subscription: { topic: string, qos: number };
publish: { topic: string, messageId?: number };
disconnect: void;
error: Error | DPSNError;
};class DPSNError extends Error {
code: DPSN_ERROR_CODES;
status?: 'connected' | 'disconnected';
constructor(options: {
code: DPSN_ERROR_CODES;
message: string;
status?: 'connected' | 'disconnected';
name?: string;
});
toJSON(): {
code: DPSN_ERROR_CODES;
message: string;
status?: 'connected' | 'disconnected';
name: string;
};
}- CONNECTION_ERROR (400): Connection issues with the DPSN broker
- UNAUTHORIZED (401): Authentication or authorization failure
- PUBLISH_ERROR (402): Error when publishing to a topic
- INITIALIZATION_FAILED (403): Client initialization failure
- CLIENT_NOT_INITIALIZED (404): Operations attempted before initialization
- CLIENT_NOT_CONNECTED (405): Operations attempted without connection
- SUBSCRIBE_ERROR (406): General subscription errors
- SUBSCRIBE_NO_GRANT (407): Permission denied for subscription
- SUBSCRIBE_SETUP_ERROR (408): Error setting up subscription handlers
- DISCONNECT_ERROR (409): Error during disconnection
- INVALID_PRIVATE_KEY (411): Invalid access token/private key
- MQTT_ERROR (413): MQTT protocol errors
This project is licensed under the ISC License.