项目作者: lukem512

项目描述 :
Simple inter-process communication using Socket.io
高级语言: JavaScript
项目地址: git://github.com/lukem512/node-socket-ipc.git
创建时间: 2016-06-13T14:42:59Z
项目社区:https://github.com/lukem512/node-socket-ipc

开源协议:

下载


Node Socket IPC

Simple socket-based inter-process communication for Node.js. This module exposes a pub/sub interface for events and allows specified routines to be remotely called. The Socket.io library is leveraged as the backbone and simple functionality implemented to allow arbitrary messages to be subscribed to.

Pub/Sub

The simple case for the socket interface is to provide a method of broadcasting messages from a publisher process to one or more subscribers. This can be used to notify the subscribers that messages have been received or computation completed.

Publisher

Publushers require the node-socket-ipc module (this one!). It can be installed by typing:

  1. npm install --save lukem512/node-socket-ipc

To publish an event, very little code is needed - it is all handled by the socket module!

  1. let socket = require('node-socket-ipc');
  2. socket.publish('test', { yourData: 'something here' });

Any event name can be specified (here test is used); if there are no subscribers the publish method takes no action. Any subscribers are sent the specified data object, in JSON, which is picked up by an event-specific handler.

Subscriber

Subscribers require the socket.io-client module. It can be installed by typing:

  1. npm install --save socket.io-client

To subscribe to an event, a socket connection must first be established, specifying the port and host of the publisher.

  1. let io = require('socket.io-client');
  2. let socket = io.connect('http://localhost:3000', { reconnect: true });
  3. // Wait for connection
  4. ebe_socket.on('connect', data => {
  5. console.log('Socket connected!');
  6. });

To subscribe to an event, a subscriber includes the following code:

  1. // Create a handler, specific to the event
  2. secure_server_socket.on('test', data => {
  3. let obj;
  4. try {
  5. obj = JSON.parse(data);
  6. console.log(obj);
  7. }
  8. catch (e) {
  9. console.error('Could not decode JSON payload');
  10. }
  11. });
  12. // Issue the subscribe message
  13. socket.emit('subscribe', { eventName: 'test' });

Remote Routines

Routine Publisher

A publisher can create routines to be called remotely. These routines must return a Promise and either resolve for correct values or reject malformed requests.

  1. // Basic function returning a Promise
  2. function yourFunction(some_param, other_param) {
  3. return new Promise((resolve, reject) => {
  4. if (!some_param || !other_param) return reject('Invalid args');
  5. resolve(some_param > other_param);
  6. });
  7. }

To expose the routine to callers, the following code is used. Any arguments must be named and are passed using an args object.

  1. // Expose the Promised function at 'testRoutine'
  2. var socket = require('node-socket-ipc');
  3. socket.routines.add('testRoutine', function(args) {
  4. return yourFunction(args.some_param, args.some_other_param);
  5. });

Routine Caller

To call a remote routine a caller creates a socket (as described previously in the Subscriber section) and emits a message containing the routineName and an object containing any named parameters. A callback function is then specified and is executed upon execution or failure of the routine.

  1. socket.emit('call', {
  2. routineName: 'testRoutine',
  3. args: {
  4. some_param: 3,
  5. some_other_param: 1
  6. }
  7. }, function(err, success) {
  8. if (err) return console.error('Error', err);
  9. if (success) {
  10. console.log(some_param + ' is greater than ' + some_other_param);
  11. }
  12. });

The callback should accept an error, err, and a return value, here success, as defined by the remote routine.