Cettia JavaScript Client A is a lightweight B JavaScript client for browser-based C and Node-based D application.

A
Cettia 1.0.0-Beta1 client.
B
53.82KB minified, 16.44KB minified and gzipped.
C
It follows jQuery's browser support that embraces Internet Explorer 9.
D
It supports Node 4+.

Quick Start

Cettia JavaScript Client is distributed through npm.

webpack, Browserify or Rollup

npm install --save cettia-client
import cettia from "cettia-client/cettia-bundler";

script tag

<script src="https://unpkg.com/cettia-client@1.0.0-Beta2/cettia-browser.min.js"></script>
window.cettia;

Node

npm install --save cettia-client
import cettia from "cettia-client";

Once you’ve loaded the module, you will be able to write the following echo and chat client. This page already loaded the uncompressed version, hence, you can run and debug it directly by copying and pasting to JavaScript console if possible.

var socket = cettia.open("http://localhost:8080/cettia");

// Lifecycle events
// When the server issues a new id for this socket as the beginning of the lifecycle and the end of the previous lifecycle
socket.on("new", function() {
    console.log("on new");
});
// When the selected transport starts connecting to the server
socket.on("connecting", function() {
    console.log("on connecting");
});
// When the connection is established successfully
socket.on("open", function() {
    console.log("on open");
});
// When an error happens on the socket
socket.on("error", function(error) {
    console.error("on error", error);
});
// When the connection is closed, regarded as closed or could not be opened
socket.on("close", function() {
    console.log("on close");
});
// When a reconnection is scheduled
socket.on("waiting", function(delay, attempts) {
    console.log("on waiting", attempts, delay);
});

// echo and chat events
socket.on("open", function() {
  // Text data
  socket.send("echo", "echo");
  socket.send("chat", "chat");
  // Binary data
  // From Encoding standard https://encoding.spec.whatwg.org/
  var encoder = new TextEncoder();
  socket.send("echo", encoder.encode("echo"));
  socket.send("chat", encoder.encode("chat"));
  // Composite data
  socket.send("echo", {text: "echo", binary: encoder.encode("echo")});
  socket.send("chat", {text: "chat", binary: encoder.encode("chat")});
  // In Node.js, use Buffer, i.e. new Buffer("echo"), to deal with binary data
});
socket.on("echo", function(data) {
    console.log("on echo", data);
});
socket.on("chat", function(data) {
    console.log("on chat", data);
});

Example

Here is working example – echo and chat