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

socketless-demo

v1.0.1

Published

Demo code for using socketless

Downloads

8

Readme

Socketless demos

This is a collection of demos that show off how to use the socketless framework for websocket client/server implementations. There are currently three demos: two simple functionality demos, and one full fledged multiplayer game.

A simple example: npm run simple

The demo starts up a server, followed by several clients. 10 seconds after joining, each client (including the webclient) will set out a chat message, and after an additional 5 seconds, it will disconnect from the server. Once the last client disconnects, the server will shut itself down.

All of this is coordinated in index.js, with the client/server API declared in the Client.js and Server.js files.

A distributed simple example: npm run distributed

This is the same test as the simple test, where each client is executed as an independent process (using the spawn command), to demonstrate things still work when the client and server code has no knowledge of each other, beyond knowing the shared API.

Like in the simple example, running of the clients and server is coordinated in index.js, and the client/server API is literally the same as the simple example (it uses those exact same files). However, instead of creating objects directly, index.js will spawn external processes that run test-server.js and test-client.js, which build the server and clients respectively, in order to demonstrate that server and client don't need to be created and run by the same script.

A simple example with web clients: npm run web

This test is similar to the simple demo, except it add web clients to the mix.

The demo starts up a server, and then starts up a web client, which a browser can connect to on http://localhost:1234, followed by a number of regular clients. Like before, 10 seconds after joining, each client (including the webclient) will set out a chat message, and after an additional 5 seconds each regular client will disconnect from the server. The web client, however, needs to be told to "quit" through its browser interface. Once all regular clients plus the web client have disconnected from the server, the server will shut itself down.

You'll notice that there is a public directory in the web demo: this is where the web client servers its index.html and style/JS from. If you look at the startWebClient() function in index.js you will see the following code:

  const webclient = ClientServer.createWebClient(
    serverURL,
    `${__dirname}/public`,
    { directSync: true }
  );

This creates a web client similar to a regular client, by telling it which URL the server is running on, but it also tells the webclient what the public directory is that it should use to server web content from. Finally, the third argument turns on direct syncing, rather than syncing through a state variable. This is mostly because it makes for easier code for a demo (see the socketless documentation on the difference between direct and state-based syncing), but if you want an example that relies on syncing through a state variable, read on...

Multiplayer mahjong: npm run game.

This is an elaborate example of how you can use socketless to implement a multiplayer game with all the bells and whistles you need, with client/web synchronisation through a "shared" state variable, with quite a lot of code for coordinating a simple "lobby" of sorts, creating, joining, and starting games, and coordinating play actions both between the clients and the server, as well as between the clients amongst themselves (using broadcasting).

The code is lavishly commented, so start reading at demo/game/index.js and branch out to other files as questions arise bout how things work.