socketcluster-wrapper-client

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

Usage no npm install needed!

<script type="module">
  import socketclusterWrapperClient from 'https://cdn.skypack.dev/socketcluster-wrapper-client';
</script>

README

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