New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@yoonit/nativescript-websocket

Package Overview
Dependencies
Maintainers
9
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yoonit/nativescript-websocket

Yoonit WebSocket plugin for Android and iOS to build modern apps with NativeScript

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
increased by9.09%
Maintainers
9
Weekly downloads
 
Created
Source

NativeScript Yoonit WebSocket

Generic badge MIT license

Build modern apps using NativeScript and WebSocket in Android and iOS

A NativeScript plugin to provide:

  • Better and more stable Websocket connection
  • Modern JS Code (ESNext)
  • VueJS integrated plugin

Installation

npm i -s @yoonit/nativescript-websocket

Usage

All the functionalities that the @yoonit/nativescript-websocket provides is accessed through the YoonitWebSocket object. Below we have the basic usage code, for more details, your can see the Methods or the Demo Vue.

VueJS Plugin

main.js

import Vue from 'nativescript-vue'
import YoonitWebSocket from '@yoonit/nativescript-websocket/vue'

Vue.use(
  YoonitWebSocket,
  'wss://echo.websocket.org/',
  {
    protocol: '',
    timeout: 1000,
    headers: [],
    reconnect: true,
    debug: false,
    proxy: {
      address: '',
      port: ''
    }
  }
)

After that, you can access the socket object in your entire project using this.$yoo.socket

Vue Component

App.vue

<template>
  <Page @loaded="doLoaded"></Page>
</template>

<script>
export default {
  data: () => ({
    interval: null
  }),
  methods: {
    doLoaded () {
      // start the connection
      this.$yoo.socket.open()

      // declare all callback events
      this.$yoo.socket.events({
        open: this.doSocketOpen,
        message: this.doReceivedMessage,
        close: this.doSocketClose,
        error: this.doSocketError
      })
    },

    doPing () {
      this.interval = setInterval(() => {
        if (!this.$yoo.socket.getStatus()) {
          return console.log('[YooSocket] Socket closed')
        }

        console.log("[YooSocket] Sending 'echo' message!")

        // add your message/file to queue and call 'send' method
        return this.$yoo.socket.push('echo')
      }, 2000)
    },

    doSocketOpen ($socket) {
      console.log("[YooSocket] Hey! I'm connected!")

      clearInterval(this.interval)

      // onOpen event calls your function
      return this.doPing()
    },

    doSocketClose () {
      // onClose event
      return console.log('[YooSocket] Socket was closed')
    },

    doSocketError () {
      // onError event
      return console.log('[YooSocket] Socket had an error')
    },

    doReceivedMessage ($socket, message) {
      // onMessage event
      return console.log(`[YooSocket] Received Message: '${message}'!`)
    },
  }
}
</script>

API

Methods
FunctionParametersValid valuesReturn TypeDescription
openAsynctimeoutany positive number, default 1000 (ms)voidWait timeout to start the connection
open--voidStart immediately the connection
onevent, callbackstring, functionvoidInclude an event, every that your server sent an event of this type, your callback will be invoked
offevent, callbackstring, functionvoidExclude an event
eventscallbacksobject with functions { open: [], close: [], message: [], error: [], fragment: [], handshake: [] }voidYou can use an Array of callbacks in each event. Use this to add a batch of events at once
getStatus--booleanYou can use this method to check the connection status
pushcontentstring/blobvoidSend files or strings to server. You can use this method to make uploads for example
destroy--voidDestroy server connection
closecode, messagenumber, stringvoidClose server connection programmatically offering an reason
queueLength--numberGet the total pending items to be sent
isOpen--booleanGet connection status
isClosed--booleanGet connection status
isClosing--booleanGet connection status
isConnecting--booleanGet connection status
Properties
PropertyReturn Type
protocolstring
timeoutstring
headersarray
reconnectboolean
debugboolean
proxyobject
callbacksobject
urlstring
openedboolean

To contribute and make it better

Clone the repo, change what you want and send PR.

Contributions are always welcome!


Code with ❤ by the Cyberlabs AI Front-End Team

Keywords

FAQs

Package last updated on 16 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc