1.1.2-beta • Published 4 years ago

nativescript-stomp-connector v1.1.2-beta

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
4 years ago

:warning: This plugin is under construction.

nativescript-stomp-connector

Client connector for Websocket through STOMP protocol.

This plugin use the NaikSoftware/StompProtocolAndroid for Android and WrathChaos/StompClientLib for iOS.

#TODO

  • Migrate to Nativescript 7;
  • Add nativescript core demo;
  • Add repository for service example using spring boot to this README;
  • Add Troubleshooting to this README;
  • Improve API documentation;

Installation

Run nativescript cli to install this plugin

tns plugin add nativescript-stomp-connector

Usage

Core

Under construction

TypeScript

Import the StompConnector in your component

import { StompConnector, StompMessage } from 'nativescript-stomp-connector';

Call Connnect

private _wsUrl = "ws://{your-server}:{port}/{path}/websocket";
public stompClient: StompConnector;

constructor(private _changeDetectorRef: ChangeDetectorRef) {
	this.stompClient = new StompConnector();
}

public connect(): void {
	this.stompClient.connect({
		brokerURL: this._wsUrl,
		onConnect: () => {
			console.log(`==== CONNECTED ===`);
		},
		onReconnect: () => {
			console.log(`==== RECONNECTED ===`);
		},
		onStompError: (error) => {
			console.error(error);
		},
		onDisconnect: () => {
			console.log(`==== DISCONNECTED ===`);
		},
		debug: (msg: string) => {
			console.log(msg);
		}
	} as StompConfig);
}

Disconnect

public disconnect(): void {
	this.stompClient.disconnect();
}

Subscribe to topic

public subscribeToTopic(): void {
	this.stompClient.topic('/topic/broadcast', (response: StompMessage) => { console.dir(response); });
}

Unsubscribe to topic

unsubscribeToTopic() {
	this.stompClient.unsubscribe('/topic/broadcast', () => console.log("Unsubscribed successfully"));
}

Send message

sendMessageAsObject() {
	this.stompClient.send({ 
		message: JSON.stringify({ content: this.messageContent }), 
		destination: '/app/greetings',
		withHeaders: { "content-type": "application/json" }
	}, () => { console.log('Message just sent!'); });
}

API

StompConnector

connect(config)

This function will connect to your stomp broker and call the callback onConnect after finish it;

disconnect()

Disconnect the stomp broker;

isConnected()

Return true if broker is connected;

topic(destination, successCallBack, failCallBack)

Subscribe to topic passing destination and successCallBack as mandatory parameters.

unsubscribe(destination, successCallback)

Unsubscribe to topic by destination.

send(request, successCallBack, failCallBack)

Send messsage to stomp broker;

StompConfiguration

PropertyDefaultDescription
brokerURLemptyThe websocket url. Mandatory
autoReconnectfalseAndroid only for now
reconnectDelay5000The delay between attempts to reconnect in ms
connectHeadersemptykey: string: string; > example: { 'content-type': 'application/json' }
onConnect()Functioncallback called on websocket connection successful
onReconnect()Functioncallback called on server auto reconnected. Android only for now
onDisconnect()Functioncallback called on webscoket disconnect
onStompError(error)Functioncalled when got an exception
debug(msg)FunctionUse this to have more logs

Native Component

AndroidiOS
NaikSoftware/StompProtocolAndroidWrathChaos/StompClientLib

Author

Daniel Borba, daniel@witfy.io

License

Apache License Version 2.0, January 2004

1.1.2-beta

4 years ago

1.1.0-beta

4 years ago

1.0.4-beta

4 years ago

1.0.2-alpha

4 years ago

1.0.1-alpha

4 years ago

1.0.0-alpha

4 years ago