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

sole-socket

v0.0.4

Published

A websocket manager for pheonix.js that guarantees one - and only one - websocket connection

Downloads

7

Readme

SoleSocket

SoleSocket is a api wrapper for the pheonix.js websockets implementation. It's main purpose is to be a fool proof way to create a socket and manage multiple channels.

It's main features are to guarantee only one socket will be established (to prevent echos), easy reconnection to a socket if it is lost (a difficult issue on mobile), and the ability to keep track of multiple channels easily.

Getting started

To get started you first need to install SoleSocket into your project:

npm install sole-socket
or
yarn add sole-socket

And then you'll need to initialize the socket instance - preferably from your app's index file (or generally anytime before you need to utilize websockets).

import { SoleSocket } from 'sole-socket';

const url = 'wss://my.socket.url/socket';

// The second parameter is simply the pheonix param: {} object
const socket = new SoleSocket(socketUrl, { jwt: 'my_token' });

socket.initialize();

And that's it! Your pheonix websocket is setup and ready to be used.

Using SoleSocket in your project

To use SoleSocket throughout your project just import the instance after you've ran socket.initialize()

import { solesocket } from 'sole-socket';

solesocket() // returns the instance of the initialized object

solesocket contains everything you need to interact directly with your websocket instance. If you need to access the the websocket you can import it with:

import { websocketSingleton } from 'sole-socket';

websocketSingleton() // returns the created websocket instance

Using the websocket directly is not recommended as a lot of the functionality provided by SoleSocket cannot be guaranteed if manipulated directly

API

initialize()

Initializes a new socket singleton instance. Will not override previously created singleton and will attempt to connect / reconnect to the socket instance. If it's already connected it will return the socket's connection state. Returns a promise.

const socket = new SoleSocket(socketUrl, { jwt: 'my_token' });

socket.initialize();

joinChannel(topic)

You can create a new channel by calling joinChannel with the initialize SoleSocket instance. The convinience here is that joinChannel() will automatically create a new channel instance and attempt to join it. If the channel already exists then the promise will resolve with a list of your current channels.

This method is also a promise and will return a list of all channels created (including your new channel) or respond with an error if creation failed.

const topic = 'fake:topic';

solesocket().joinChannel(topic).then((channels) => {
  console.log(channels[topic]) // prints out the 'fake:topic' channel
}).catch((err) => {
  // catches any errors thrown by joining a channel
});

subscribeToChannelEvent(topic, event, callback)

Subscribes to an event on a specified channel. New events received will trigger a callback function.

const topic = 'fake:topic';
const event = 'fake_user_message';
const callback = () => {console.log('Hello Event!')}

solesocket().subscribeToChannelEvent(topic, event, callback);

// On callback triggers 'Hello Event!'

sendMessage(topic, event, data)

Sends a message to the specified channel. Returns a promise with the data returned from socket.

const topic = 'fake:topic';

solesocket().sendMessage(topic, 'event', 'new message!');

leaveChannel(topic)

Leaves a specified channel and removes it from the channels list.

const topic = 'fake:topic';

solesocket().leaveChannel(topic);

purge()

Disconnects from the socket and removes the singleton reference. Completely resets SoleSocket.

solesocket().purge();

Publishing Package

To publish new versions of habitat first add you newest changes and increment the verison number inside of package.json.

Once that is done, commit your changes to git and create a new tag. For example:

git add new_file.txt && git commit -m "Add new file"
git tag 1.0.0
git push origin 1.0.0
git push origin master

After the tag is added to git you need to publish the package to npm by running

npm publish

This will automatically create a new build of the code and push directly to the npm package repository.

License

MIT