Client connector for Websocket through STOMP protocol.
This plugin use the NaikSoftware/StompProtocolAndroid for Android and WrathChaos/StompClientLib for iOS.
- [ ] 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;
Run nativescript cli to install this plugin
tns plugin add nativescript-stomp-connector
Under construction
Import the StompConnector in your component
import { StompConnector, StompMessage } from 'nativescript-stomp-connector';
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);
}
public disconnect(): void {
this.stompClient.disconnect();
}
public subscribeToTopic(): void {
this.stompClient.topic('/topic/broadcast', (response: StompMessage) => { console.dir(response); });
}
unsubscribeToTopic() {
this.stompClient.unsubscribe('/topic/broadcast', () => console.log("Unsubscribed successfully"));
}
sendMessageAsObject() {
this.stompClient.send({
message: JSON.stringify({ content: this.messageContent }),
destination: '/app/greetings',
withHeaders: { "content-type": "application/json" }
}, () => { console.log('Message just sent!'); });
}
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;
Property | Default | Description |
---|---|---|
brokerURL | empty | The websocket url. Mandatory |
autoReconnect | false | Android only for now |
reconnectDelay | 5000 | The delay between attempts to reconnect in ms |
connectHeaders | empty | [key: string]: string; > example: { 'content-type': 'application/json' } |
onConnect() | Function | callback called on websocket connection successful |
onReconnect() | Function | callback called on server auto reconnected. Android only for now |
onDisconnect() | Function | callback called on webscoket disconnect |
onStompError(error) | Function | called when got an exception |
debug(msg) | Function | Use this to have more logs |
Android | iOS |
---|---|
NaikSoftware/StompProtocolAndroid | WrathChaos/StompClientLib |
Daniel Borba, daniel@witfy.io
Apache License Version 2.0, January 2004