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

dok-socket

v1.0.7

Published

Uses socket.io to handle data sharing easily via sockets.

Downloads

3

Readme

dok-socket

Uses socket.io to handle data sharing easily via sockets.

This is the server component.


  • Server package:

    • NPM: https://www.npmjs.com/package/dok-socket

    • Github: https://github.com/jacklehamster/dok-socket

  • Client package:

    • NPM: https://www.npmjs.com/package/dok-socket-client
    • Github:

Setup server on Node.js:

const { serveSocket } = require('dok-socket');

const app = express();
const { io, server } = serveSocket(app);
const httpServer = server.listen(port, () =>
   console.log(`Listening on port ${port}!`.bgGreen));

This opens the socket on the local server.

Then import the client (use browserify to get the Nodejs module working in browser).

Create main.js

const { SocketClient } = require("dok-socket-client");

module.exports = {
		SocketClient,
};

Browserify:

browserify main.js --standalone dok-lib > json-compact.js

Use:

const { 
	SocketClient,
} = dokLib;

const socket = new SocketClient(backupServer);

Now you can use socket to share data or call functions in real time.

Join / leave a room

//	To join a room:
socket.join("room");

//	To leave a room:
socket.leave("room");

As soon as you join a room, data gets shared across.

Sharing data

Once you joined a room, a simple way to handle real-time communication is to have all clients share a piece in a big pool of data.

//	To share data with other clients in the room
socket.update(data);

Data can be any serializable object. Then you can access all data shared within the room:

//	This is the number of data available
const count = socket.dataCount();

//	Then iterate through all the data:
for (let i = 0; i < count; i++) {
		const data = socket.getSharedDataAt(i);
  	//	if you need, you can use the client's id:
  	const id = socket.getIdAt(i);
  	const dataById = socket.getSharedData(id);

  	//	...
}

Note data socket.getSharedData(0) is your own data. If you haven't set it, it will be empty ({});

You can just read data in a loop repeatedly, or listen to data change:

socket.addEventListener("update", (id, data) => {
	//	do something with id and data
});

Traditionally, we have each client share a model that describe a character in a game. Each client continously update their own client, and processes data for all clients including self, to display the various characters in a game.

Call methods accross clients in real-time

Another way to handle real-time communication is to call methods across several clients. Calling a method on one client would call the exact same method on all clients automatically.

obj.method = socket.wrap("name", obj.method);

Now, when you call obj.method, it will execute whatever obj.method was doing, but it will also call obj.method on all other clients (assuming all clients also ran the same code that wraps their own obj.method under name).

For instance:

console.log = socket.wrap("log", console.log);

console.log("Hello everybody!");	//	This logs the messsage "Hello everybody!" on everyone's console, including your own.

This is an easy way to use socket.io. If you have your own use case, I would suggest you code your own implementation using socket.io. The package is available here: https://socket.io/