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

A
Cettia 1.0.0 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 and 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/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. FYI, this page already loaded the uncompressed version. Open the JavaScript console and type cettia.

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

// Lifecycle events
// When the selected transport starts connecting to the server
socket.on("connecting", function() {
    console.log("on connecting");
});
// When the server issues a new id for this socket as the beginning of the new lifecycle and the end of the previous lifecycle
socket.on("new", function() {
    console.log("on new");
});
// 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 atempt 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