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

socketconnect

v0.0.1

Published

WebSocket wrapper with HTTP repeat request fallback, providing a shared API for sending and receiving data.

Downloads

6

Readme

SocketConnect

Incredibly simple and extendable WebSocket wrapper with an HTTP repeat request fallback providing a shared API for sending and receiving data. SocketConnect checks if WebSockets are available to the client, if so they perform as normal, if not it falls back to repeat HTTP requests. You can then compare responses and check for new data.

Documentation

Import or load the module

var SocketConnect = require('socketconnect');

import SocketConnect from 'socketconnect';

Create a new instance

var connection = new SocketConnect(websocketURL, httpFallbackURL);

If your websocketURL works as a HTTP fallback by replacing ws:// with http:// there's no need for the second argument as SocketConnect will perform the replacement. However if that's not the case, the second argument is necessary for the HTTP fallback.

WebSocket / HTTP Shared API

Sending messages

connection.send(data);

Handling data

connection.handleData = function(e) {
  console.log('Data:', e);
};

Set up custom handling environment

There are two flags for checking whether your SocketConnect has used websockets or the fallback. Use some logic like below to separate your custom event handling:

if (connection.isWebSocket) {
  // Custom WebSocket handling here
} else if (connection.isHTTP) {
  // Custom HTTP handling here
}

The below API is split is split into WS/HTTP, make sure you override them within the correct block of the above logic.

WebSocket

Event handling

You'll want to override the native WebSocket event handlers, you can do this on connection.ws.*event*, e.g.:

connection.ws.onopen = function() {
  console.info('Connected!');
};

The same applies for onerror, onclose, however you'll handle onmessage within the shared API handleData.

IMPORTANT: You must set connection.canSend = true; within the onopen event, this ensures you cannot send messages before the connection is established.

HTTP

SocketConnect falls back to HTTP repeat requests using XMLHttpRequest.

Setting request interval

The request interval is set to 1000(ms) by default, but you can change that using:

connection.setReqInterval(5000); // 5 Seconds

Event Handling

SocketConnect provides access to all status updates from XMLHttpRequest. You can add any event handler to them using the below:

// Response code 0
connection.reqNotInit = function() {
  console.log('Request not initialised.');
};

| Response Code | Meaning | SocketConnect Function | | ----------------|------------------------------------| -----------------------| | 0 | Request not initialised | reqNotInit | | 1 | Server connection established | connectionEstablished | | 2 | Request received | reqReceived | | 3 | Processing request | processingReq |

Response code 4 is dealt with within the shared API handleData.