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

chat-client

v1.0.3

Published

a simple chat client to be used with node-chat-server

Downloads

81

Readme

chat-client

A simple chat client to be used with node-chat-server.

Installation

npm install chat-client

Usage

This chat client is compliant and meant to be used with node-chat-server.

var ChatClient = require('chat-client');

// creating a ChatClient and connecting the undelying socket.
var chatClient = new ChatClient({ url: 'ws://localhost:4001' });

// you can also connect at a later time:
  
chatClient = new ChatClient();

chatClient.connect('ws://localhost:4001');

// wait for the socket to be opened.
chatClient.whenOpened(()=>{
  
  // get authorization from the chat server.
  // the token here is just an example as the actual authorization protocol is completely up to you.
  chatClient.authorize({ token: 123 }).then(()=>{
    
    // get some old messages
    chatClient.getMessages({ with: 'someUserId', limit: 10, skip: 0 }).then((chatMessages) => {
    
      console.log('some old chat messages:', chatMessages);
      
    }).catch((err)=>{
    
      console.log('failed to get messages', err);
      
    });
    
    // listen for incoming chat messages.
    chatClient.on('chatMessage', (msg)=>{
    
      console.log('got message:', msg);
      
      // mark a message as having been read. 
      chatClient.read(msg._id).then(ok => /* ok */ ).catch(err => /* oops.. */ );
      
    });
    
    // create a new chat message.
    var newMessage = {
      to: 'someUserOrGroupId',   // required property.
      isGroup: true,             // the server needs to know if this message is for a group.
      type: 'text',              // the rest is up to you..
      value: 'Hi!'
    };
    
    chatClient.send(newMesssage).then((msg) => {
    
      console.log('message sent');
      
    }).catch((err) => {
    
      console.log('failed to send message', err);
      
    });
    
  }).catch(()=>{
  
    console.log('authorization failed!');
    
  });
  
});

Options

When creating an instance of ChatClient you may pass an 'options' object to it, containing some or all of the following fields:

  • url - String - if preset will connect the underlying socket to url.
  • onOpen - Function - will be called when the socket opens.
  • onError - Function - will be called when the socket cannot connect or is closed unexpectedly.
  • onMessage - Function - will be called on any incoming socket message.
  • onClose - Function - will be called whenever the socket closes.

Methods

  • connect - (url: String) - connects the underlying socket to url.
  • on - (eventName: String, listener: Function) - binds listener to the eventName event.
  • off - (eventName: String, listener: Function) - unbinds listener to the eventName event. if listener is not provided, all listeners for eventName will be removed. if both listener and eventName are not provided, all listeners for all events will be removed.
  • emit - (eventName: String, ...args) - emit the eventName event. any additional arguments will be passed to bound listeners.
  • run - (type: String, data: Object) => Promise - runs an action on the server. type is the name of the action on the server. data will be passed to the action on the server. a deffered promise is returned from this function. the promise will be resolved or rejected based on the server action's response.
  • whenOpened - (callback: Function) - runs callback whenever the socket opens. note that if the socket is already opened, callback will run immediately.
  • authorize - (data: Object) - runs the authorize action on the server, passing data.
  • send - (message: Object) - sends a chat message to the server. this will run the create action on the server, passing data.
  • read - (data: Object) - mark a chat message as having been read. this will run the read action on the server, passing data.
  • error - (err: any) - a default error handler which just logs the error to the console. if you pass onError to the constructor it will override this function.

Events

  • message - fired for every incoming socket message.
  • open - fired whenever the socket openes.
  • close - fired whenever the socket closes.
  • error - fired when the socket cannot connect or is closed unexpectedly.
  • chatMessage - fired for every incoming chat message.