nativescript-signalr
A NativeScript plugin to connect and talk with a signalr server
npm i --save nativescript-signalr
  • Version: 1.0.0
  • GitHub:
  • Downloads:
    • Last Day:   0
    • Last Week:  0
    • Last Month: 0

npm npm npm

nativescript-signalr

A NativeScript plugin to connect and talk to a C# signalr server

WARNING: The code is NOT on NPM yet, the code is only available to Patreon supporters. THIS is on NPM to be used as a Placeholder so that this plugin does have a home when it is finally released.

License

The actual code is released under what I call the PATRON License, meaning you are free to include this in any type of program as long as you downloaded this when you were a current patron and/or sponsor, and got the file directly from my patreon post. It will be released under a proper open source license at a time of my choosing (typically after a couple months of exclusive access by sponsor's). At that point the product will be re-released under the MIT license. -- However for entities that need a support contract, changes, enhancements and/or a commercial license please contact me at http://nativescript.tools.

I also do contract work; so if you have a module you want built for NativeScript (or any other software projects) feel free to contact me nathan@master-technology.com.

Donate Patreon

Installation

Download from the patreon link

tns plugin add nativescript-signalr-1.0.0.tgz

Usage

To use the module you just require() it, and then tell it to connect to your server:

var signalr = require( "nativescript-signalr" );

// Send Protocol (i.e. http or https), Server Name, signalr data, websocket setup function
signalr.createSignalRConnection("http", "127.0.0.1", '[{"name":"chathub"}]',
function(err, mySocket) {
// This function allows you to attach any event listeners to the socket before it is opened that you might need.
mySocket.addEventListener('open', function (evt) {
console.log("We are Open!");
});
mySocket.addEventListener('message', function (evt) {
console.log("Message contains", evt.type);
if (Object.prototype.toString.call(evt.data) === "[object ArrayBuffer]") {
console.log("We got binary message");
} else {
console.log("We got a message: ", evt.data);
self.set("message", evt.data);
}
});
mySocket.addEventListener('close', function (evt) {
console.log("The Socket was Closed:", evt.code, evt.reason);
});
mySocket.addEventListener('error', function (evt) {
console.log("The socket had an error", evt.error);
});
}).then(function(socket) {
console.log("Connected to our socket");
// Do something with the open socket if need be...
socket.send({m:"We are here"});
}).catch(function(err) {
console.log("error", err);
});

Function

createSignalRConnection(protocol, url, data, setupWSHandlers)

protocol - http or https
url - the url of your site; i.e. signalr.mydomain.com
data - the data you need to send to the server (i.e. like channel)
setupWSHandlers - a callback function to allow you to setup any WebSocket handlers that you might need.

WebSocket Handlers, are "error", "close", "message" and "open"

The socket returned in the promise (and the setupWSHandlers) supports the standard web socket commands like:

  • .close()
  • .send()
  • .readyState

Please see the nativescript-websockets documentation for any specifics.

Demo information

You need to run a signalr sample chathub server app; and then you can use this sample app to connect to the server. If you choose to use a different signalr demo; you can set the demo app to use that channel by changing the data: '[{"name":"chathub"}]' in the main-view-model to whatever name the main signalr server channel is called.

The sample app is rather simple; it basically connects on the first tap of the button. The status line in the app will show the last message received and the console will output each message. If you also connect via a web browser to the server you can see the message the app sent and the app will see any messages that the browser sent showing you that it is fully connected as a signalr client. In addition if you click the "Tap" button while connected it will send a simple "Hi" message to the server.

All the sample code is in the main-view-model.js