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

socketiyo

v8.0.1

Published

WebSocket-based, bi-directional events in between client and server

Downloads

41

Readme

socketiyo

WebSocket-based, bi-directional events in between client and server.

  • WebSocket
  • Events
  • Auto-reconnect
  • Light-weight
  • Modern

Platforms

  • socketiyo (server): NodeJs
  • socketiyo-client: NodeJs, Web, Deno

Usage

Install

server side

npm i socketiyo

client side

npm i socketiyo-client

Server

import ws from "ws";
import {
    attachWebSocketServer,
    CONNECT,
    DISCONNECT,
    ERROR,
    DEFAULT_CHANNEL,
} from "socketiyo";
import {
    maxClients,
    highClients,
    lowEnough,
    maxLength,
    maxChannels,
    maxChannelLength,
} from "socketiyo/source/defaultOptions.js";
import {useDefaultLogging} from "socketiyo/source/defaultLogging.js";



// see https://github.com/GrosSacASac/JavaScript-Set-Up/blob/master/js/network/socketiyo/examples/exampleServer.js
// to view example on how to create the variable webSocketServer
const socketiYoServer = attachWebSocketServer({
    webSocketServer
    maxClients,
    highClients,
    lowEnough,
    maxLength,
    maxChannels,
    maxChannelLength,
});

useDefaultLogging({socketiYoServer});

/* send the current time on the default channel to everyone */
setInterval(() => {
    socketiYoServer.sendAll(Date.now());
}, 1500);

socketiYoServer.on(CONNECT, socket => {
    /* send welcome to the socket*/
    socketiYoServer.send(socket, { message: `welcome` });
    /* alert others as well */
    socketiYoServer.sendAllExceptOne(socket, { message: `new connection` });
});

socketiYoServer.on(`game/input`, ({socket, data}) => {
    console.log(`${socket} send us ${data}`);
});

Server APIs

socketiYoServer = attachWebSocketServer({
    webSocketServer,
    highClients,
    maxClients,
    maxLength,
    maxChannels,
    maxChannelLength,
    lowEnough,
    packData = defaultPackData,
    unpackData = defaultUnpackData,
})
socketiYoServer.on(eventName, callback)
socketiYoServer.off(eventName, callback)
socketiYoServer.send(socket, data, channel = DEFAULT_CHANNEL)
socketiYoServer.sendAll(data, channel = DEFAULT_CHANNEL)
socketiYoServer.sendAllExceptOne(exceptionSocket, data, channel = DEFAULT_CHANNEL)
socketiYoServer.close()

Extensions

Disconnection detection

By default a TCP connection can stay open for an indefinite amount of time. And WebSockets are built on top of TCP. If one end of the connection disconnects abruptly it will not properly send the end connection message. The server will not know it disconnected and may keep the connection information in memory. If this happens too much the memory leak may eventually crash the application. The disconnection detection is an extension that will periodically send PING and expect a PONG response, to confirm that the connection that the server has in memory are indeed still alive.

import { useAdditionalDisconnectionDetection } from "socketiyo/extensions/disconnectionDetection.js";


const closeDisconnectionDetection = useAdditionalDisconnectionDetection({ socketiYoServer });

The closeDisconnectionDetection is a function that should be called before the websocket server is closed.

Client

Import

Node
import {
    createConnection,
    CONNECT,
    DISCONNECT,
    ERROR,
    DEFAULT_CHANNEL,
    reconnectDelay, randomReconnectDelay, autoReconnect,
    useDefaultLogging,
} from "socketiyo-client/source/socketiyo-client.js";
Deno and Web

Use the node import if you are using a bundler

import {
    createConnection,
    CONNECT,
    DISCONNECT,
    RECONNECTING,
    ERROR,
    DEFAULT_CHANNEL,
    reconnectDelay,
    randomReconnectDelay,
    autoReconnect,
    useDefaultLogging,
} from "./node_modules/socketiyo-client/built/socketiyo-client.es.js";
// or } from "https://unpkg.com/socketiyo-client/built/socketiyo-client.es.js";

usage


const socketiyoConnection = createConnection({
    url: `ws://localhost:8080/socketiyo`,
    reconnectDelay,
    randomReconnectDelay,
    autoReconnect,
});

useDefaultLogging({ socketiyoConnection });

socketiyoConnection.on(DEFAULT_CHANNEL, (x) => {
    console.log(x);
});

socketiyoConnection.on(`game/end`, (x) => {
    console.log(`game/end ${x}`);
});

Client APIs


socketiyoConnection = createConnection({
    url,
    packData = defaultPackData,
    unpackData = defaultUnpackData,
    reconnectDelay,
    randomReconnectDelay,
})
socketiyoConnection.on(eventName, callback)
socketiyoConnection.off(eventName, callback)
socketiyoConnection.send(data, channel = DEFAULT_CHANNEL)
socketiyoConnection.close()

About

Concept inspired by socket.io

What are the differences between socketiyo and socket.io ?

  • Rooms and events are combined and called channels.
  • Namespaces are handled outside of the library by creating more instances with different options.
  • The WebSocket http server implementation is not provided, instead socketiyo expects a peer dependency to be injected at runtime.
  • The client is not served by default in the server. It has to be explicitly imported in the client code instead.
  • No fallback provided when WebSocket is not available. No background protocol upgrades.
  • Regular events and library events cannot be confused.
  • Does not decorate the sockets with custom methods

License

CC0