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-event

v1.1.2

Published

Easy websockets usage.

Downloads

7

Readme

socket-event

WebSocket client-server communication library.

About

WebSoket technology is awesome. But it comes a little hard to start using it. Also you have some problems building big apps with websockets.

In this library we still have imitation of methods (get, post, put, delete), added new one "notify". The connection goes with websockets and we have ability to set callback on front and handler on back. Also added one method "notify". It should serve to send any kind of notifications between front and end. You can build client => server event system on top of this "notify" method.

Actually all these methods work identically. The idea of separating them is to make code better and cleaner. The logic you build around them is your deal.

Usage:

Front:

Include the front-end lib.The lib is placed in the /lib folder:

<script src="socket-events.js" type="text/javascript"></script>

Init the socket. You may pass port as a parameter. The default port used is 8081.

var socket = new SocketEvent(optionalPort)

Using send methods(get, post, put, delete, notify) has same interface.

socket[methodName](eventName, data, callback)

All arguments are optional instead of eventName.

socket.get('messages', null, function(data) {
  console.log(data)
})

socket.post('message', data)

socket.put('message', data, function(data) {
  console.log(data);
})

socket.delete('message', data)

// Using notify for small messages looks good idea

socket.notify('state-change', data)

socket.notify('game-over', data)

socket.notify('user-afk', data)

Server can send notifications too. To make it easier, the server sends only the name of the event. How you handle it on front:

socket.on('map-refresh', function() {
  // Server notifies about map refresh
  // You send request for the new map
  socket.get('map', null, function(data) {
    // Print new map
    console.log(data);
  })
})

Also, using method 'on', you can set handler to the 'open' event of the socket. Its reserved word. 'Open' event fires when the socket connection between server and client is established and ready to function normally. Example:

socket.on('open', function() {
  console.log("socket has just connected")
})

Or you can simply react on the certain events:

socket.on('server-off', function() {
  alert("Server will be shutdown in 2 minutes. Please, save your work.")
})

You also may delete handlers of the server events dynamically using method "remove":

socket.remove('server-off')

Back:

The model of interaction looks like: Event(route, url) - Handler - Callback

The handler is actually callback too. Just not to get confused, we will call it handler. So, handler is actually the event handler on back-end, same as common router handler. Firing callback - is sending data/notification to front-end.

Include this module by:

npm install --save socket-event

Init socket server. Three possible ways to do that: pass port, pass server, dont pass any:

const s = require('socket-event')


// Example: set connection port to 8000
var options = { port : 8000 }

// Other example: set connection to the created before http server
var options = { server : server}

// This will set to default port 8081
var options = null

s.init(options, function(ws) {
  // Port is optional. Default is 8081.
  // Any additional logic can be placed here
  // Actually this is on 'connection' event handler, that will be fired on connection with each socket
  // ws - socket object, you may atach any logic to it. But remember, socket refreshes on page refresh, browser close etc...
})

Set handlers. Callback has two "node-way" arguments: error, data.

s.get('messages', function(data, callback) {
  let msg = JSON.parse(data);  

  // connect to db and get data. example code
  let sql = "SELECT * FROM `messages` WHERE id = " + msg.data.id;
  someDbDriver.query(sql, function(err, data) {
    if(err) callback(err);

    else callback(null, data);
  }   
});

s.post('message', function(data, callback) {
  // pretty much the same

 // dont forget to fire callback(err,data) at the end
});

// same for s.put, s.delete

s.notify('any-event', function(data, callback) {
  // You can build here any logic outside restapi
});

You might need to delete handler dynamically. Use 'remove' method:

s.remove('get', 'greet');
s.remove('post', 'greet');
s.remove('put', 'greet');
s.remove('delete', 'greet');
s.remove('notify', 'greet');

The server to client communication is done through the "notifyAll" method. This code notifies all connected clients about event.

s.notifyAll('map-refresh')

Happens, that you need to get the original socket object. You may access it with method "getSocket".

let socket = s.getSocket()

// Maybe atach some user object, session or any other logic