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

socket-emitter

v1.5.6

Published

A TCP Socket Tool, it make sending a message by tcp just like emitting an event

Downloads

132

Readme

Socket-Emitter

A TCP Socket Tool, it make sending a message by tcp just like emitting an event.

It can send most common type of data without any extra work.

It's able to send large data.

Data will be received after 50ms.

Server Example:

var Server = require('socket-emitter').Server;

var PORT = 3766;
var options = {
  // compress: 'zlib',
}

var server = new Server(options);
server.on('connection', function(socket) {
  console.log('[Server]New Connection');

  socket.on('message', function(message) {
    console.log(message);
    // message will be 2
  });

  socket.emit('sendString', 'hello world');

  socket.emit('sendObject', {
    message: '123',
    count: 23
  });

  var buffer = new Buffer(4);
  buffer.writeUInt32BE(0x1234, 0);
  socket.emit('sendBuffer', buffer);

  socket.emit('endSend');
});
server.listen(PORT, function() {
  console.log('Server Started');
});

Client Example:

var Client = require('socket-emitter').Client;

var PORT = 3766;
var options = {
  // compress: 'zlib',
}

var client = new Client(options);
client.connect(PORT, 'localhost', function() {
  console.log('Client Connected');
  client.emit('message', 2);
});

client.on('sendString', function(message) {
  console.log(message);
  // message will be ‘hello world’
});

client.on('sendObject', function(message) {
  console.log('message');
  // message will be {message: '123', count: 23}
});

client.on('sendBuffer', function(message) {
  console.log(message);
  // message will be <Buffer 00 00 12 34>
});

client.on('endSend', function(message) {
  console.log(message);
  // message will be {}
  client.close(function() {
    console.log('Client Closed');
  });
});

Sample API


SendableDataType

Socket is able to send most of the data types, such as String, Number, Buffer, Object, Array, null, and others. If the data's type is Object, each of it's property value can be a SendableDataType data, such as :

{
  str: 'abcdef',
  num: 23,
  buffer: new Buffer(10),
  array: [1, 'abc', /* and others*/],
  // and others
}

If the data's type is Array, each entry of the Array can be a SendableDataType data, such as :

[
  'abc',
  {obj: true},
  new Buffer(20),
  [1, 2, 3],
  // and others
]

new Server([options])

Create a Server instance with options.

options

Type: Object

[options.compress = null]

If options.compress is equal to 'zlib', Socket will compress data before send.

Sometimes it will be slower than sending data without compress.

Server's Event

connection

Server will emit a connection event when there is a new connection.

The only argument is a Socket, you can communicate with Client through it.

error

Server will emit an error event when there is an error.

others

Server will emit other event in different situations.

Server.listen

It's used to listen Client's connection, is same as net.Server.listen.

Server.broadcast(event, arg[, except])

Send data to all clients.

event

Type: String

arg

Type: SendableDataType

except

Type: Socket | Array of Socket

Server will not send data to these Sockets.

Server.sockets

It an array of online Clients.

Server.sockets.on(event ,listener)

A method to receive data.

event

Type: String

listener

Type: Function(socket, arg)

Function will be called when any Socket has received a data and event is same as sender's event argument. The first argument socket is the socket witch had received this data.


new Client([options])

Create a Client instance with options, options is same as above.

Client is a subclass of Socket, the only extra method is connect.

Client.connect

It's used to connect to Server, is same as net.Socket.connect.


Socket.emit(event[, arg])

Method to send data.

event

Type: String

arg

Type: SendableDataType

Socket.on(event, listener)

Method to receive data.

event

Type: String

listener

Type: Function(arg)

Function will be called when Socket has received a data and event is same as sender's event argument.


More info to see at socket-emitter