method-web-socket-expose

1.0.64 • Public • Published

ws: a node.js websocket library

method-web-socket-expose is a simple to use ws implementation.

Installing

npm install --save method-web-socket-expose

Server defining a method

var ws = require('method-web-socket-expose');
var router = require('method-web-socket-expose').Router();
var server = new ws.start({host:"localhost", port: 8000});

router.setMethod('serverMethod', function (_return) {
	_return(["What's Up!"], "Bye");
});
server.use(router);

Client calling server method

var ws = require('method-web-socket-expose').client;
var client = ws.connect('ws://localhost:8000');

client.on('ready', function(){
  client.serverMethod("tellmeSomething").then(function(res,err){
  	console.log(arguments);
  });
});

Server executing a method on client side.

var ws = require('method-web-socket-expose');
var server = new ws.start({host:"localhost", port: 8000});

server.on('connection', function(client){
	setTimeout(function(){
		client.execute('clientMethod',["hello", "Bye"]);
	}, 3000);
});

Client defining a method

var ws = require('method-web-socket-expose').client;
var client = ws.connect('ws://localhost:8000');

client.setMethod('clientMethod', function (response) {
	console.log(response);
});

ExpressJS example

Start the web socket on http express server, in the file www

var app = require('../app');
var ws = require('method-web-socket-expose');

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
/**
 * Start server listening 
 **/
ws.getServer('my-ws-server').start(server,"my-ws-server");
.....

ExpressJS example

Using the ws server in app.js

var express = require('express');
var ws = require('method-web-socket-expose');

/**
 * Create Server instance.
 *  *Without starting listen.
 **/
var wsServer = ws.start({},'my-ws-server');

/*
* EXPOSE ROUTER METHOS TO WEB SOCKET
*/
wsServer.use( require('./routes/session') );
wsServer.use( require('./routes/employ') );
wsServer.use( require('./routes/person') );
wsServer.use( require('./routes/management') );
.....

Server sending broadcast data

var ws = require('method-web-socket-expose');
var server = new ws.start({host:"localhost", port: 8000});

server.on('connection', function(client){
	client.execute('clientMethod',["hello","bye"]);
});

server.broadcast = function broadcast(data) {
  server.clients.forEach(function each(client) {
    client.send(data);
  });
};

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.640latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.640
1.0.630
1.0.620
1.0.610
1.0.600
1.0.590
1.0.580
1.0.570
1.0.560
1.0.550
1.0.540
1.0.530
1.0.520
1.0.510
1.0.500
1.0.490
1.0.480
1.0.470
1.0.460
1.0.450
1.0.440
1.0.430
1.0.420
1.0.410
1.0.40
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i method-web-socket-expose

Weekly Downloads

0

Version

1.0.64

License

ISC

Last publish

Collaborators

  • gabryk