cettia / cettia-javascript-client

A lightweight JavaScript client for browser-based and Node-based Cettia applications

Home Page:https://cettia.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cettia JavaScript Client

Cettia JavaScript Client is a lightweight JavaScript client for browser-based and Node-based application and distributed via the npm registry with the name, cettia-client.

Installation

script tag

Add the following script tag to your HTML.

<script src="https://cdn.jsdelivr.net/npm/cettia-client/cettia-browser.min.js"></script>
window.cettia;

Bundler

Install and load the package.

npm install cettia-client
var cettia = require('cettia-client/cettia-bundler');

Note: You should use this way to use Cettia in React Native.

Node

Install and load the package.

npm install cettia-client
var cettia = require('cettia-client');

Usage

Cettia will be familiar to people using other real-time web frameworks and libraries. You open a socket and send and receive events with Cettia's API. Here is the main.js contents which is a Node example of the Cettia Starter Kit.

const cettia = require("cettia-client");
const username = "DH";
const uri = `http://localhost:8080/cettia?username=${encodeURIComponent(username)}`;
const socket = cettia.open(uri);

const addMessage = ({sender, text}) => console.log(`${sender} sends ${text}`);
socket.on("message", addMessage);

const addSystemMessage = text => addMessage({sender: "system", text});
socket.on("connecting", () => addSystemMessage("The socket starts a connection."));
socket.on("open", () => addSystemMessage("The socket establishes a connection."));
socket.on("close", () => addSystemMessage("All transports failed to connect or the connection was disconnected."));
socket.on("waiting", (delay) => addSystemMessage(`The socket will reconnect after ${delay} ms`));

// Once a socket has opened, sends a message every 5 seconds
socket.once("open", () => setInterval(() => socket.send("message", {text: `A message - ${Date.now()}`}), 5000));

Besides a Node example, the Cettia starter kit provides Web example and React Native example which demonstrate how you can use Cettia JavaScript Client in various runtime environment.

See Also

About

A lightweight JavaScript client for browser-based and Node-based Cettia applications

https://cettia.io

License:Apache License 2.0


Languages

Language:JavaScript 100.0%