Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cafehub-client

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cafehub-client

CafeHub websocket client

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

cafehub-client

Custom WebSocket client for CafeHub.

Connect to the backend

Let's say you have an instance of cafehub WebSocket server running on ws://192.168.0.10:8765. We can connect to it using CafeHubClient#connect(url[, options]) method. Example:

import CafeHubClient from 'cafehub-client'

const ch = new CafeHubClient()
​
async function connect() {
    await ch.connect('ws://192.168.0.10:8765', {
        retry: 3,
    })
}

connect()

Connect to DE1

We can combine it with scanning and connecting to a DE1 machine.

import CafeHubClient from 'cafehub-client'
import {
    Device,
    isScanResultUpdate,
    RequestCommand,
    UpdateMessage,
} from 'cafehub-client/types'

const ch = new CafeHubClient()

async function autoPair() {
    console.log('Connecting to cafehub…')

    await ch.connect('ws://192.168.0.10:8765', {
        retry: 3,
    })

    console.log('Looking for DE1…')

    const msg: UpdateMessage = await client.sendRequest(
        {
            command: RequestCommand.Scan,
            params: {
                Timeout: timeout,
            },
        },
        {
            resolveIf(msg) {
                if (!isScanResultUpdate(msg)) {
                    return false
                }

                return !msg.results.MAC || msg.results.Name === 'DE1'
            },
        }
    )

    if (!isScanResultUpdate(msg) || !msg.results.MAC) {
        throw new Error('DE1 was not found.')
    }

    console.log('Connecting cafehub to DE1…')

    const msg: UpdateMessage = yield client.sendRequest({
        command: RequestCommand.GATTConnect,
        params: {
            MAC: msg.results.MAC,
        },
    })

    console.log('Done.')
}

autoPair()

At this point we're ready to send other instructions to the CafeHub instance.

tbc.

FAQs

Package last updated on 29 Nov 2022

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