tube-socket
v1.0.5
Published
Your gateway to tubes.dev development
Downloads
17
Readme
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.isConnectedA boolean value if the client is currently connected.TubeSocket.isLeaderA boolean value if the client is currently the leader of all the clients.TubeSocket.clientCountA count of clients that are all connected to the current channel.TubeSocket.oncloseAn event listener to be called when the connection is closed.TubeSocket.onerrorAn event listener to be called when an error occurs.TubeSocket.onmessageAn event listener to be called when a message is received from the server.TubeSocket.onopenAn event listener to be called when the connection is opened.TubeSocket.urlThe 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 atofield, before enqueing the data to be transmittedTubeSocket.sendJSONToLeader(object)Serializes an object and adding atofield 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.
closeFired when a connection with a TubeSocket is closed. Also available via the onclose propertyerrorFired 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.messageFired when data is received through a TubeSocket. Also available via the onmessage property.openFired when a connection with a TubeSocket is opened. Also available via the onopen property.
