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

message-engin

v0.0.4

Published

message engin.

Downloads

7

Readme

message-engin

message engin!

##installation

$ npm install message-engin --save

##Design

We can define our own message protocol.

##Basic useage:

###server

'use strict';
var co = require('co'),
http = require('http');

var server  = require('message-engin').server;

function genKey(obj){
	return obj.dn+":"+obj.appid;
}

/**
give the key to the connection
*/
function* keyFn(request){
	//request.cookies  // you can get cookie
	var httpRequest = request.httpRequest;
	console.log("key:",genKey(httpRequest.headers),httpRequest.headers);
	return genKey(httpRequest.headers);
}

/*
calc the connetion key from the message
*/
function messageKeyFn(message , headers){
	console.log('message key' , message,headers);
	return genKey(headers);
}
function createHttpServer(){
	var httpServer = http.createServer(function(request, response) {
	    console.log((new Date()) + ' Received request for ' + request.url);
	    response.writeHead(404);
	    response.end();
	});
	httpServer.listen(8080, function() {
	    console.log('Server is listening on port 8080');
	});
	return httpServer;
}
co(function*(){
	//message queue config
	var mqConf = {host: 'localhost'};
	//websocket config
	var wsConf ={httpServer:createHttpServer() , keyFn:keyFn,messageKeyFn:messageKeyFn };
	yield server.start(mqConf , wsConf);
})();

###client

'use strict';
var co = require('co');
var Client  = require('message-engin').Client;
co(function*(){
	var conf = {host: 'localhost'};
	var client = new Client(conf);
	yield client.init();
	client.on('message' , function(message,headers){
		console.log(message,headers);
		//echo client
		client.send(message,headers);
	});
	client.on('status' , function(message,headers){
		console.log('status change',message,headers);
	});
})();

ws client

'use strict';
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
    console.log('WebSocket client connected');
    connection.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });
    connection.on('close', function() {
        console.log('Connection Closed');
    });
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            var msg = JSON.parse(message.utf8Data);
            console.log("Received: " + msg.content,"+"+(Date.now()-msg.ct+'ms'));
        }
    });
    function sendMessage() {
        if (connection.connected) {
            var number = Math.round(Math.random() * 0xFFFFFF);
            connection.sendUTF(JSON.stringify({dn:'dn1' , appid:"appid1", ct:Date.now() ,content:number.toString()}));
            setTimeout(sendMessage, 1000);
        }
    }
    sendMessage();
});
client.connect('ws://localhost:8080' , null , null , {dn:"dn1",appId:"appid1"});

##API

Server

var server = require('message-engin').server;

Methods

function* start(mqConf , wsConf)

we must yield server.start(mqConf , wsConf) to start server.mqConf ref:node-amqp. ####wsConf propperties wsConf ref:WebSocket-Node. wsConf has two extra items:

  • function* keyFn(request) - function , keyFn give the connection key to identify a connection. request.httpRequest is the http request at websocket handshake.so we can use request.httpRequest.headers to get http headers.
  • messageKeyFn(message,headers) - give the connection key from message to determine which connection we will send the message. the headers is come from client.send(message,headers).

Client

Constructor

###Client(mqConf) var Client = require('message-engin').Client Create a message engin client. mqConf ref:node-amqp

Methods

function* init()

We must yield client.init() to init client;

Messages

message

we get a new message , client.on('message' , function(message,headers){}). the header is come from a websocket http request at handsake. ###status connnection status change,such as a new connection established or a connection disconneted. client.on('message' , function(message,headers){}).the header is come from a websocket http request at handsake.When server accept a new connection ,we will receive {status:1,key:"connection key"}.when connection disconnect ,we will receive {status:0 , key:"connection key"}