socketcluster-wrapper-client
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

Socketcluster Client Wrapper

Usages

Web

// for browser
import 'socketcluster-wrapper-client/dist';

// for react or other import module
import SocketClient from 'socketcluster-wrapper-client';
const options = {
  secure: true,
  authType: 'ws'
}
// options.secure to use https
// options.authType: using authentication via http or ws.

// apiKey: indentity client request can generate from dashboard
const client = new SocketClient(apiKey, options)
const socket = await client.connect()

// other usage can follow https://github.com/SocketCluster/socketcluster-client#transmit-data

React Native

import SocketClient from 'socketcluster-wrapper-client';
import AuthEngine from 'socketcluster-wrapper-client/authEngine';

const options = {
  secure: true,
  authType: 'ws',
  authEngine: new AuthEngine()
}
// options.secure to use https
// options.authType: using authentication via http or ws.

// apiKey: indentity client request can generate from dashboard 
const client = new SocketClient(apiKey, options)
const socket = await client.connect()

// Connect with channel and listen stream data
// using for await (let data of channel) is not working with React Native
const channel = socket.subscribe('realtime');

const iterator = channel[Symbol.asyncIterator]();
const result = [];
while (result.length < Infinity) {
    const { value, done } = await iterator.next();
    if (done) break;
    console.log(value);
}

// other usage can follow https://github.com/SocketCluster/socketcluster-client#transmit-data

Additional to support es6 async iterable

  yarn add --dev @babel/plugin-proposal-async-generator-functions
  npm install --save-dev @babel/plugin-proposal-async-generator-functions

Add Line bellow to babel.config.js

  "plugins": ["@babel/plugin-proposal-async-generator-functions"]

Socket Listener

const channel = socket.subscribe('realtime');
for await (let data of channel) {
  console.log(data);
}

NOTE Additional installation AsyncStorage

Readme

Keywords

none

Package Sidebar

Install

npm i socketcluster-wrapper-client

Weekly Downloads

1

Version

1.1.6

License

MIT

Unpacked Size

129 kB

Total Files

11

Last publish

Collaborators

  • bunhouth