Needless Patchouli Manufacture
    Wondering what’s next for npm?Check out our public roadmap! »

    socketiyo

    7.0.2 • Public • Published

    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";
    
    
    
    
    import { createHttpServer } from "./createHttpServer.js"; // see examples
    const socketiYoServer = attachWebSocketServer({
        httpServer,
        ws,
        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({
        httpServer,
        ws,
        path,
        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

    Install

    npm i socketiyo

    DownloadsWeekly Downloads

    11

    Version

    7.0.2

    License

    CC0-1.0

    Unpacked Size

    26.3 kB

    Total Files

    9

    Last publish

    Collaborators

    • avatar