boco-socket-rpc
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 = serverSocketon "connection" server = socket: socket serverregisterMethod "hello" done null"Hello, !" serverregisterMethod "throwIt" throw error # client clientSocket = client = socket: clientSocket # mock connecting serverSocketemit "connection"clientSocket
Calling a Method
The result of the remote method is returned asynchronously.
clientcallMethod "hello""client" throw error if error? expectresulttoEqual "Hello, client!" ok
Errors thrown by the remote method are returned as well.
clientcallMethod "throwIt""foo!" expectresulttoEqual undefined expecterrortoEqual "foo!" ok
Sending a Request
You can send a json-rpc-formatted request manually using the sendRequest
method:
clientsendRequest method: "hello"params: "client" throw error if error? expectresulttoEqual "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:
clienton "error" expecterrorcodetoEqual -32600 expecterrormessagetoEqual "Invalid Request" expecterrordatarequestmethodtoEqual null ok clientsendRequest method: null throw Error"Should not get here"
MethodNotFound
A MethodNotFound
error will be emitted if the requested method was not registered by the server:
clienton "error" expecterrorcodetoEqual -32601 expecterrormessagetoEqual "Method not found" expecterrordatarequestmethodtoEqual "goodbye" ok clientsendRequest method: "goodbye"params: 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" -->
A noConflict
method is provided that will reset the global object and return the module instance:
// file: "/js/app.js" { var BocoSocketRPC = rootBocoSocketRPC; var socket = ; var 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" -->
// file: "/js/app.jsrequirejs; ;
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.