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);
  });
};

Package Sidebar

Install

npm i method-web-socket-expose

Weekly Downloads

0

Version

1.0.64

License

ISC

Last publish

Collaborators

  • gabryk