# nativescript-socketio

> Socket.IO for nativescript

Latest version **3.3.1** (published 2019-06-02) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install nativescript-socketio
pnpm add nativescript-socketio
yarn add nativescript-socketio
bun add nativescript-socketio
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.3.1 |
| Published | 2019-06-02 |
| First published | 2016-04-09 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 428.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Osei Fortune |
| Maintainers | db3dev, roblav96, triniwiz |
| Keywords | nativescript, ecosystem:nativescript, nativescript-android, nativescript-ios, NativeScript, JavaScript, Android, IOS, socket.io, socketio, io, socket, events, tcp, websocket, framework, realtime |

## Links

- npm: https://www.npmjs.com/package/nativescript-socketio
- Homepage: https://github.com/triniwiz/nativescript-socketio
- Issues: https://github.com/triniwiz/nativescript-socketio/issues
- npm.io page: https://npm.io/package/nativescript-socketio

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.3.1 (latest) — 2019-06-02
- 3.3.0 — 2019-05-29
- 3.2.1 — 2018-09-23
- 3.2.0 — 2018-09-23
- 3.1.5 — 2018-09-18
- 3.1.4 — 2018-08-30
- 3.1.3 — 2018-08-25
- 3.1.2 — 2018-08-25
- 3.1.1 — 2018-08-24
- 3.1.0 — 2018-08-21
- 3.0.2 — 2018-07-30
- 3.0.1 — 2018-07-30
- 3.0.0 — 2018-07-30
- 2.5.0 — 2018-05-17
- 2.4.0 — 2017-06-11
- … 27 more at https://npm.io/package/nativescript-socketio/versions

## README

[![npm](https://img.shields.io/npm/v/nativescript-socketio.svg)](https://www.npmjs.com/package/nativescript-socketio)
[![npm](https://img.shields.io/npm/dt/nativescript-socketio.svg?label=npm%20downloads)](https://www.npmjs.com/package/nativescript-socketio)
[![Build Status](https://travis-ci.org/triniwiz/nativescript-socketio.svg?branch=master)](https://travis-ci.org/triniwiz/nativescript-socketio)
# nativescript-socketio
# Usage

```
npm install nativescript-socketio
```

or

```
tns plugin add nativescript-socketio
```

## NativeScript Core

Set connection string and options then connect

```js
var SocketIO = require('nativescript-socketio').SocketIO; 
var socketIO = new SocketIO(url, opts);
```
Alternatively:
```js
import { SocketIO } from 'nativescript-socketio';
var socketIO = new SocketIO(url, opts);
```

Connect to server
```js
socketIO.connect()
```

Send data to the server
```js
socketIO.emit(event,data)
```
Listen for data 
```js
socketIO.on(event,callback)
```
Set instance
```js
new SocketIO(null,null,oldInstance)
```

## Angular

``` ts
// app.module.ts
import { SocketIOModule } from "nativescript-socketio/angular";

@NgModule({
  imports: [
    SocketIOModule.forRoot(server),
  ]
})
```

``` ts
// 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();
  }
}
```

``` ts
// 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
``` bash
cd demo/demo-server
npm install
node app
```

## Api

| Method                                   | Default | Type                         | Description                                           |
| ---------------------------------------- | ------- | ---------------------------- | ----------------------------------------------------- |
| constructor(url) |         | `void`                     | Creates a SocketIO instance with a url |
| constructor(url, options:{}) |         | `void`                     | Creates a SocketIO instance with url and options|
| constructor(null,null,nativeSocket) |         | `void`                     | Creates a SocketIO instance from a native socket instance |
| connect()                    |         | `void`                 | Connect to the server.                  |
| disconnect()                    |         | `void`                 | Disconnects the socket.                   |
| connected()   |         | `boolean` | Checks if the socket is connected                              |  |
| on(event: string,(data: Object , ack? : Function))                       |         | `Function`                       | Adds a handler for a client event. Return a function to remove the handler.                             |
| once(event: string,(data: Object , ack? : Function))                       |         | `Function`                       | Adds a single-use handler for a client event. Return a function to remove the handler.                             |
| off(event: string)                        |         | `void`                       | Removes handler(s) based on an event name.                               |
| emit(event: string,data: {},ack?: Function)                      |         | `void`                       | Send an event to the server, with optional data items.                  |
| joinNamespace(name: string)                      |         | `SocketIO`                       | Return SocketIO instance with the namespace                   |
| leaveNamespace()                      |         | `void`                       | Call when you wish to leave a namespace and disconnect this socket.                  |

## Example Image
![socketio](https://i.imgur.com/4xi4h3r.gif)

---
_Source: https://npm.io/package/nativescript-socketio · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
