npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phonesocket

v1.0.7

Published

[GitHub](https://github.com/88potatoes/phonesocket) JS package for connecting to a server via websocket from phone and desktop. The package allows you to differentiate between phone connections and desktop connections. Also includes reconnection handling.

Downloads

3

Readme

Phonesocket

GitHub JS package for connecting to a server via websocket from phone and desktop. The package allows you to differentiate between phone connections and desktop connections. Also includes reconnection handling.

XSocketServer

XSocketServer is a child class of WebSocketServer from 'ws'. It provides extra functionality:

  • All phones which connect to an XSocketServer are automatically registered as phone devices
  • All desktops which connect to and XSocketServer are automatically registered as desktop devices
# To use import it like
import { XSocketServer } from 'phonesocket/xserver';

Properties and methods

XSocketServer.register_event(device, event, callback) Registers an event to be sent by a particular kind of device and the corresponding callback

device = 'phone' | 'desktop'
event: String - name of event sent from client
callback = (ws, data) => {...}

XSocketServer.broadcast_phones(event, data) Sends a JSON websocket message of the form {"command": [event], "data": [data]} to all phone connections

event: String
data: any

XSocketServer.broadcast_desktops(event, data) Sends a JSON websocket message of the form {"command": [event], "data": [data]} to all phone connections

event: String
data: any

Example

const xsocketserver = new XSocketServer({port: 8082});

// when a phone sends an event 'toggle_reset'
xsocketserver.register_event('phone', 'toggle_reset', (ws, data) => {
    xsocketserver.broadcast_phones('reset_state', 'not_ready')
    xsocketserver.broadcast_desktops('newgame', Object.keys(coins)) // coins is an object
    ws_send(ws, 'reset_state', true ? 'ready' : 'not_ready')
})

XSocketServer.onclose_extra A function that is run upon websocket disconnection

XSocketServer.onconnect A function that is run upon websocket connection

Example

// some dummy code taken from a project
xsocketserver.onclose_extra = (ws) => {
    latent_players[ws.ip] = players[ws.id];
    delete players[ws.id];
}

xsocketserver.onconnect = (ws, req) => {
    if (ws.device === "desktop") {
        sendJSON(ws, 
        {command: "init_players", data: players}, 
        {command: "init_walls", data: walls}, 
        {command: "init_coins", data: Object.values(coins)});
    } else {
        //...
    }
}

XSocketClient

Client side websocket object Use a tool such as watchify to use node package in client side

Properties and methods

XSocketClient.register_event(event, callback) Registers an event and a correspondign callback Example


const phonesocket = new XSocketClient('phone', `ws://localhost:8082`)
const resetbutton = document.getElementById('resetbutton');
phonesocket.register_event('reset_state', (data) => {
    resetbutton.innerText = data === 'ready' ? 'READY' : '';
})

Functions from index.js

sendJSON(ws, ...messages) Sends JSON messages through websocket See above XSocketServer.onconnect for example

ws_send(ws, event, payload) Also sends JSON messages through websocket Example

ws_send(ws, 'reset_state', true ? 'ready' : 'not_ready')

get_id() Get a unique id - from 0 to 10000 (maxed out at 10000 so not technically not unique); is used internally to differentiate websocket connections.