Package Exports
- nativescript-socketio
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nativescript-socketio) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
nativescript-socketio
Note 3.x uses socket.io 2.0+
Usage
NativeScript 4x
tns plugin add nativescript-socketio
NativeScript 2 - 3x
tns plugin add nativescript-socketio@2.5.0
Nativescript Core
Set connection string and options then connect
var SocketIO = require('nativescript-socketio').SocketIO;
var socketIO = new SocketIO(url, opts);Alternatively:
import { SocketIO } from 'nativescript-socketio';
var socketIO = new SocketIO(url, opts);Connect to server
socketIO.connect()Send data to the server
socketIO.emit(event,data)Listen for data
socketIO.on(event,callback)Set instance
new SocketIO(null,null,oldInstance)Angular
// app.module.ts
import { SocketIOModule } from "nativescript-socketio/angular";
@NgModule({
imports: [
SocketIOModule.forRoot(server),
]
})// app.component.ts
import { Component, OnInit, OnDestroy } from "@angular/core";
import { SocketIO } from "nativescript-socketio";
@Component({
// ...
})
export class AppComponent implements OnInit, OnDestroy {
constructor(private socketIO: SocketIO) { }
ngOnInit() {
this.socketIO.connect();
}
ngOnDestroy() {
this.socketIO.disconnect();
}
}// test.component.ts
import { Component, OnInit, NgZone } from "@angular/core";
import { SocketIO } from "nativescript-socketio";
@Component({
// ...
})
export class TestComponent implements OnInit {
constructor(
private socketIO: SocketIO,
private ngZone: NgZone
) { }
ngOnInit() {
this.socketIO.on("test", data => {
this.ngZone.run(() => {
// Do stuff here
});
});
}
test() {
this.socketIO.emit("test", { test: "test" });
}
}Running Demo
Start socketio server
cd demo/demo-server
npm install
node appScreenshot
