npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

livegollection-client

v0.2.2

Published

livegollection-client is the JavaScript/TypeScript client-side library to interact with the correspondant server-side Golang library livegollection that you can find on GitHub at m1gwings/livegollection .

Downloads

8

Readme

livegollection-client

livegollection-client is the JavaScript/TypeScript client-side library to interact with the correspondant server-side Golang library livegollection.

If you haven't done it yet, check out livegollection before continuing to read this page.

Install

livegollection-client is distributed on npm but you can also find the source code on GitHub.

The simplest way to add livegollection-client to your project is:

npm install livegollection-client

How to use

In this section we'll assume that you have installed livegollection-client from npm.

Import

livegollection-client default exports the LiveGollection class as an ES2015 module. You can simply import it in your script:

import LiveGollection from "path/to/node_modules/livegollection-client/dist/index.js"; 

Create the LiveGollection object

To interact with server-side livegollection we need to crate a LiveGollection object. LiveGollection constructor takes an url as a parameter, this url points to the livegollection websocket handler: in fact you must use ws:// or wss:// in the URL protocol field. The domain of the url is the domain of your server (localhost if you're testing the project in local). The path of the url depends on the route where you have set the livegollection handler.

For example if you have written this on your server executable:

// ...
liveGoll := livegollection.NewLiveGollection(//...
// ...
http.HandleFunc("/livegollection", liveGoll.Join)
http.ListenAndServe("localhost:8080", nil)
// ...

the correspondant url is ws://localhost:8080/livegollection so you can create the LiveGollection object as follows:

let liveGoll = new LiveGollection("ws://localhost:8080/livegollection");

NOTE: You must use TLS if you want to connect with a websocket to a remote server (in other words you can use ws:// only when you are testing in local).

Set event handlers

Every time an item in the collection is created, updated or deleted the LiveGollection object created in the previous step gets notified from the server and will invoke the correspondant event handler among oncreate, onupdate or ondelete.

We need to set the appropriate code for each one of this event handlers (updates WON'T be processed until every handler has been set):

liveGoll.oncreate = (item) => {
    // Add element to DOM
};

liveGoll.onupdate = (item) => {
    // Modify element in the DOM
};

liveGoll.ondelete = (item) => {
    // Delete item from the DOM
};

There are also two others event handlers: onopen and onclose invoked when the websocket underlying connection has been opened/closed. You don't need to set them but they could be useful.

Interact with the collection

The proper way to interact with the collection are the three methods provided by the LiveGollection object: create, update and delete. Every one of these methods takes just one parameter: an item to create/update/delete.

NOTE: When you invoke one of the method above the server receives the update and will send it to EVERY client in the pool, also to the one that has emitted the update, so you don't need to update the DOM when you call such methods.

Suppose that you have defined some buttons to create, update or delete an item, you can do the following:

createButton.onclick = () => {
    liveGoll.create({
        // Fill fields of the item object, you don't need to set the id, the server will take care of that
        fieldOne: // ...
        fieldTwo: // ...
        // ...
    });
}
// ...
updateButton.onclick = () => {
    itemToUpdate.fieldToUpdate = newValue;
    liveGoll.update(itemToUpdate);
}
// ...
deleteButton.onclick = () => {
    liveGoll.delete(itemToDelete);
}

As mentioned in the comment, when you create an item you don't need to set its id, the server will take care of that.

Example app

If you want a concrete example of usage of livegollection and livegollection-client check out this example app.