boco-socket-rpc

0.3.3 • Public • Published

boco-socket-rpc

npm version npm license dependencies

Easy JSON RPC over Web Sockets with socket.io

Table of Contents

Installation

Installation is available via npm, bower or github.

$ npm install boco-socket-rpc
$ bower install boco-socket-rpc
$ git clone https://github.com/bocodigitalmedia/boco-socket-rpc

Usage

Although you will most likely use this library with socket.io, it can be used with any EventEmitter. To demonstrate this, our examples will use an event emitter in place of the actual socket.io server and client.

BocoSocketRPC = require "boco-socket-rpc"
EventEmitter = require("events").EventEmitter
 
# server 
serverSocket = new EventEmitter()
 
serverSocket.on "connection"(socket) ->
  server = new BocoSocketRPC.Server socket: socket
 
  server.registerMethod "hello"(sender, done) ->
    done null"Hello, #{sender}!"
 
  server.registerMethod "throwIt"(error, done) ->
    throw error
 
# client 
clientSocket = new EventEmitter()
client = new BocoSocketRPC.Client socket: clientSocket
 
# mock connecting 
serverSocket.emit "connection"clientSocket

Calling a Method

The result of the remote method is returned asynchronously.

client.callMethod "hello""client"(error, result) ->
  throw error if error?
  expect(result).toEqual "Hello, client!"
  ok()

Errors thrown by the remote method are returned as well.

client.callMethod "throwIt""foo!"(error, result) ->
  expect(result).toEqual undefined
  expect(error).toEqual "foo!"
  ok()

Sending a Request

You can send a json-rpc-formatted request manually using the sendRequest method:

client.sendRequest method: "hello"params: ["client"](error, result) ->
  throw error if error?
  expect(result).toEqual "Hello, client!"
  ok()

note: a uuid value is automatically assigned to the id of all requests

Request Errors

The following request errors may be emitted to the client.

InvalidRequest

An InvalidRequest error will be emitted if the data provided is not a valid Request object:

client.on "error"(error) ->
  expect(error.code).toEqual -32600
  expect(error.message).toEqual "Invalid Request"
  expect(error.data.request.method).toEqual null
  ok()
 
client.sendRequest method: null(error, result) ->
  throw Error("Should not get here")

MethodNotFound

A MethodNotFound error will be emitted if the requested method was not registered by the server:

client.on "error"(error) ->
  expect(error.code).toEqual -32601
  expect(error.message).toEqual "Method not found"
  expect(error.data.request.method).toEqual "goodbye"
  ok()
 
client.sendRequest method: "goodbye"params: [](error, result) ->
  throw Error("Should not get here")

Browser Usage

An UMD module is included in this package at /dist/boco-socket-rpc.js. A minified version is also available.

Using the Browser Global

<!-- file: "index.html" -->
<script src="/socket.io/socket.io.js"></script>
<script src="/js/boco-socket-rpc.js"></script>
<script src="/js/app.js"></script>

A noConflict method is provided that will reset the global object and return the module instance:

// file: "/js/app.js"
(function(root) {
 
  var BocoSocketRPC = root.BocoSocketRPC.noConflict();
  var socket = io("http://localhost:3000");
  var client = new BocoSocketRPC.Client({ socket: socket });
  // ...
 
})(this);

Using require.js

boco-socket-rpc is AMD compatible, and can be used with AMD loaders like require.js.

<!-- file: "index.html" -->
<script data-main="/js/app.js" src="/js/require.js"></script>
// file: "/js/app.js
requirejs.config({
  paths: {
    "socket.io": "/socket.io/socket.io.js",
    "boco-socket-rpc": "/js/boco-socket-rpc.js"
  }
});
 
require(["socket.io", "boco-socket-rpc"], function(io, BocoSocketRPC) {
  var socket = io("http://localhost:3000");
  var client = new BocoSocketRPC.Client({ socket: socket });
  ok();
});

The MIT License (MIT)

Copyright (c) 2015 Christian Bradley + Boco Digital Media, LCC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependents (0)

Package Sidebar

Install

npm i boco-socket-rpc

Weekly Downloads

1

Version

0.3.3

License

MIT

Last publish

Collaborators

  • christianbradley