tube-socket

1.0.5 • Public • Published

TubeSocket

A library to support tubes! This wraps a WebSocket functionality and can be used as a websocket with some changes!

Instead of a URL you can just pass your room ID.

import TubeSocket from 'tube-socket';
 
const conn = new TubeSocket('F3A00E');
 
conn.addEventListener('message', (evt) => {
  const payload = JSON.parse(evt.data);
  switch(payload.action) {
    case 'chat_message':
      console.log(payload.message);
      break;
  }
});

Features

  • Reconnection. When disconnected, the library will automatically reconnect in 5 seconds and keep trying until a connection is made.
  • Direct Messages. The library adds functionality that will allow clients to directly message each other by using conn.sendJSONTo
  • Leadership. Since you can use tubes.dev without a backend, you need a single source of truth. This library adds a leadership algorithm to sync up the next feature

Installation

npm i tube-socket --save-dev

Constructor

  • TubeSocket(roomID) Returns a newly created TubeSocket object.

Properties

  • TubeSocket.isConnected A boolean value if the client is currently connected.
  • TubeSocket.isLeader A boolean value if the client is currently the leader of all the clients.
  • TubeSocket.clientCount A count of clients that are all connected to the current channel.
  • TubeSocket.onclose An event listener to be called when the connection is closed.
  • TubeSocket.onerror An event listener to be called when an error occurs.
  • TubeSocket.onmessage An event listener to be called when a message is received from the server.
  • TubeSocket.onopen An event listener to be called when the connection is opened.
  • TubeSocket.url The absolute URL of the TubeSocket.

Methods

  • TubeSocket.send(string) Enqueues data to be transmitted.
  • TubeSocket.sendJSON(object) Serializes an object before enqueing the data to be transmitted.
  • TubeSocket.sendJSONTo(client_id, object) Serializes an object and adding a to field, before enqueing the data to be transmitted
  • TubeSocket.sendJSONToLeader(object) Serializes an object and adding a to field directly to the leader, before enqueing the data to be transmitted

Events

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

  • close Fired when a connection with a TubeSocket is closed. Also available via the onclose property
  • error Fired when a connection with a TubeSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.
  • message Fired when data is received through a TubeSocket. Also available via the onmessage property.
  • open Fired when a connection with a TubeSocket is opened. Also available via the onopen property.

Readme

Keywords

Package Sidebar

Install

npm i tube-socket

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

33.7 kB

Total Files

18

Last publish

Collaborators

  • timanema