This is a JavaScript classes documentation which describes both client and server instance creation and management.
- Client
- Constructor
- connect
- registerMethod
- registerInternalMethod
- unregisterMethod
- unregisterInternalMethod
- getRegisteredMethodsNames
- getRegisteredInternalMethodsNames
call- callMethod
- callInternalMethod
listMethods- listRemoteMethods
- listRemoteEvents
notify- sendNotification
- sendInternalNotification
- registerNotification
- registerInternalNotification
- unregisterNotification
- unregisterInternalNotification
- getRegisteredNotifications
- getRegisteredInternalNotifications
- onNotification
- onInternalNotification
- onceNotification
- onceInternalNotification
- offNotification
- offInternalNotification
- subscribe
- unsubscribe
- close
- getRPCSocket
- event:open
- event:error
- event:close
- event:RPCConnection
- Server
- Constructor
register- registerMethod
- registerInternalMethod
- unregisterMethod
- unregisterInternalMethod
- getRegisteredMethodsNames
- getRegisteredInternalMethodsNames
event- registerNotification
- registerInternalNotification
- unregisterNotification
- unregisterInternalNotification
- getRegisteredNotifications
- getRegisteredInternalNotifications
- sendNotification
- sendInternalNotification
- onNotification
- onInternalNotification
- onceNotification
- onceInternalNotification
- offNotification
- offInternalNotification
eventList- of
- createError
- createNamespace
- hasNamespace
- getNamespace
- getOrCreateNamespace
- closeNamespace
- close
- event:listening
- event:connection
- event:error
- Namespace
registerevent- name
connectedeventListclients- registerNotification
- registerInternalNotification
- unregisterNotification
- unregisterInternalNotification
- getRegisteredNotifications
- getRegisteredInternalNotifications
- sendNotification
- sendInternalNotification
- onNotification
- onInternalNotification
- onceNotification
- onceInternalNotification
- offNotification
- offInternalNotification
- registerMethod
- registerInternalMethod
- unregisterMethod
- unregisterInternalMethod
- getRegisteredMethodsNames
- getRegisteredInternalMethodsNames
- getClients
- getClient
- close
- JSONRpcSocket
Departing from version 2.x, there's been some minor API changes. A breaking change is a server.eventList method, which is not a getter method anymore, because of the inclusion of a namespaces system throughout the library. Other methods will work seamlessly.
var WebSocket = require('rpc-websockets').Client
var ws = new WebSocket('ws://localhost:8080')
Instantiate a WebSocket client.
Parameters:
address
{String}: The URL of the WebSocket server. The URL path portion resolves to a server namespace. If the URL query keysocket_id
exists, it will be used as a socket identifier. Defaults to 'ws://localhost:8080'.options
{Object}: Client options that are also forwarded tows
.autoconnect
{Boolean}: Client autoconnect upon Client class instantiation. Defaults totrue
.reconnect
{Boolean}: Whether client should reconnect automatically once the connection is down. Defaults totrue
.reconnect_interval
{Number}: Time between adjacent reconnects. Defaults to1000
.max_reconnects
{Number}: Maximum number of times the client should try to reconnect. Defaults to5
.0
means unlimited.strict_subscriptions
{Boolean}: should special request to server be send to confirm client's subscription to some event(s). Defaults totrue
.
generate_request_id
{Function} Custom function to generate request id instead of simple increment by default. Passesmethod
andparams
to parameters.
Connects to a previously defined server if not connected already. Returns promise which resolves once connection was opened. Should only be used in case autoconnect
was disabled.
For more information see registerMethod of Namespace
For more information see registerInternalMethod of Namespace
For more information see unregisterMethod of Namespace
For more information see unregisterInternalMethod of Namespace
For more information see getRegisteredMethodsNames of Namespace
For more information see getRegisteredInternalMethodsNames of Namespace
DEPRECATED, use callMethod or callInternalMethod instead
Calls a registered RPC method on server. Resolves once the response is ready. Throws if an RPC error was received.
Parameters:
method
{String}: An RPC method name to run on server-side.params
{Object|Array}: Optional parameter(s) to be sent along the request.timeout
{Number}: Optional RPC reply timeout in milliseconds.ws_options
{Object}: Optional parameters passed to ws. Not available on web browsers.compress
{Boolean}: Specifies whether data should be compressed or not. Defaults to true when permessage-deflate is enabled.binary
{Boolean}: Specifies whether data should be sent as a binary or not. Default is autodetected.mask
{Boolean} Specifies whether data should be masked or not. Defaults to true when websocket is not a server client.fin
{Boolean} Specifies whether data is the last fragment of a message or not. Defaults to true.
For more infomation see callMethod of JSONRpcSocket
For more infomation see callInternalMethod of JSONRpcSocket
DEPRECATED, use listRemoteMethods instead
Fetches a list of client's methods registered on server.
For more infomation see listRemoteMethods of JSONRpcSocket
For more infomation see listRemoteEvents of JSONRpcSocket
DEPRECATED, use sendNotification or sendInternalNotification instead
Sends a JSON-RPC 2.0 notification to server.
Parameters:
method
{String}: An RPC method name to run on server-side.params
{Object|Array}: Optional parameter(s) to be sent along the request.
For more information see sendNotification of Namespace
For more information see sendInternalNotification of Namespace
For more information see registerNotification of Namespace
For more information see registerInternalNotification of Namespace
For more information see unregisterNotification of Namespace
For more information see unregisterInternalNotification of Namespace
For more information see getRegisteredNotifications of Namespace
For more information see getRegisteredInternalNotifications of Namespace
For more information see onNotification of Namespace
For more information see onInternalNotification of Namespace
For more information see onceNotification of Namespace
For more information see onceInternalNotification of Namespace
For more information see offNotification of Namespace
For more information see offInternalNotification of Namespace
Subscribes for a defined event(s). On fulfilled returns object with event names as keys and subscription status in values ("ok" of successfully subscribed, or string with error information if subscription failed)
Parameters:
event
{String|Array}: Event name.
Unsubscribes from a defined event(s). On fulfilled returns object with event names as keys and subscription status in values ("ok" of successfully unsubscribed, or string with error information if unsubscription failed)
Parameters:
event
{String|Array}: Event name.
For more information see close of JSONRpcSocket
Emits when the connection is opened and ready for use.
- <Error>
Emits when a socket error is raised.
Emits when the connection is closed.
var WebSocketServer = require('rpc-websockets').Server
var server = new WebSocketServer({
port: 8080,
host: 'localhost'
})
Instantiate a WebSocket server.
Parameters:
options
{Object}: Server options that are also forwarded tows
.port
{Number}: Port number on which the server will listen for incoming requests.host
{String}: Address on which the server will listen for incoming requests.strict_notifications
{Boolean}: Should server send events to only those clients that previously subscribed to this events. Defaults totrue
idParam
{String}: name of the parameter in request which is used to determine connected socket ID. Defaults to"socket_id"
Once the Server class is instantiated, you can use a ws
library's instance via server.wss object.
DEPRECATED, use server.registerMethod or server.registerInternalMethod instead
Registers an RPC method and returns the RPCMethod object to manage method permissions.
Parameters:
method
{String}: RPC method name.handler
{Function}: RPC function that will be fired with a possible parameter object once the method is called.namespace
{String}: Namespace identifier. Defaults to/
.
Registers a method on given namespace.
For more information see registerMethod of Namespace
Registers an internal method on given namespace.
For more information see registerInternalMethod of Namespace
Unregisters a method on given namespace.
For more information see unregisterMethod of Namespace
Unregisters an internal method on given namespace.
For more information see unregisterInternalMethod of Namespace
Returns list of registered methods on given namespace
For more information see getRegisteredMethodsNames of Namespace
Returns list of registered internal methods on given namespace
For more information see getRegisteredInternalMethodsNames of Namespace
DEPRECATED, use server.registerNotification or server.registerInternalNotification instead
Creates a new event that can be emitted to clients.
Parameters:
name
{String}: Name of the event.namespace
{String}: Namespace identifier. Defaults to/
.
Registers given notification on given namespace
For more information see registerNotification of Namespace
Registers given internal notification on given namespace
For more information see registerInternalNotification of Namespace
Unregisters given notification on given namespace
For more information see unregisterNotification of Namespace
Unregisters given internal notification on given namespace
For more information see unregisterInternalNotification of Namespace
Returns list of notifications names registered on given namespace
For more information see getRegisteredNotifications of Namespace
Returns list of internal notifications names registered on given namespace
For more information see getRegisteredInternalNotifications of Namespace
For more information see sendNotification of Namespace
For more information see sendInternalNotification of Namespace
For more information see onNotification of Namespace
For more information see onInternalNotification of Namespace
For more information see onceNotification of Namespace
For more information see onceInternalNotification of Namespace
For more information see offNotification of Namespace
For more information see offInternalNotification of Namespace
DEPRECATED, use server.getRegisteredNotifications or server.getRegisteredInternalNotifications
Lists all created events.
Parameters:
namespace
: Namespace identifier. Defaults to/
.
Returns a Namespace object initialized by the provided pathname upon connecting (eg: /chat
).
Defaults to /
.
Parameters:
name
{String}: Namespace identifier.
More information on Namespaces below.
Creates a structured error that can be thrown in a .register callback.
Parameters:
code
{Number}: Indicates the error type that occurred.message
{String}: Provides a short description of the error.data
{String|Object}: Details containing additional information about the error.
Creates new namespace on the server
Parameters:
name
{String}: name of namespace
Checks whether server has a namespace with given name
Parameters:
name
{String}: name of namespace
Returns a namespace with given name
Parameters:
name
{String}: name of namespace
Helper function that returns existing or creates new namespace with given name
Parameters:
name
{String}: name of namespace
Closes the given namespace and terminates all its clients.
Closes the server and terminates all clients.
Returns instance of JSONRpcSocket with given ID or undefined if no socket found
Parameters
id
{String|Number}: socket ID
Emits when the server has started listening for requests.
socket
<ws.WebSocket>request
<http.IncomingMessage>
Emits when the client has connected.
- <Error>
Emits when a server error is raised.
socket
<JSONRpcSocket>request
<http.IncomingMessage>
Emits when the client has connected and RPC connection is initialized.
Namespace represents a pool of sockets connected under a given scope identified by a pathname (eg: /chat
). Basically borrows ideas from socket.io
.
DEPRECATED, use registerMethod or registerInternalMethod of Namespace
A convenience method for server.register using this namespace.
**DEPRECATED, use registerNotification or registerInternalNotification of Namespace
A convenience method for server.event using this namespace.
Returns a namespace identifier.
DEPRECATED
Returns a hash of websocket objects connected to this namespace, identified by id
.
**DEPRECATED, use getRegisteredNotifications of Namespace instead.
A convenience method that lists all created events in this namespace.
DEPRECATED, use getClients of Namespace instead
Returns a list of client unique identifiers connected to this namespace.
Registers a notification that can be send to clients.
Parameters:
name
{String}: name of notification
Registers an internal notification that can be send to clients. The only difference between this method and registerNotification is that this method allows to register notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String}: name of notification
// "rpc.on" notification will be registered in both cases:
namespace.registerInternalNotification('rpc.on');
namespace.registerInternalNotification('on');
// And this will throw an error:
namespace.registerNotification('rpc.on')
Unregisters given notification.
Parameters:
name
{String}: name of notification
Unregisters given internal notification. The only difference between this method and unregisterNotification is that this method allows to unregister notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String}: name of notification
Returns list of registered notifications names (not including internal notifications)
Returns list of registered internal notifications names
Sends a notification with given name and parameters to subscribed clients
Parameters:
name
{String}: name of notificationparams
{Array|Object}: notifications parameters. If object passed notifications will be delivered in a by-name fashion.
Sends an internal notification with given name and parameters to subscribed clients. The only difference between this method and sendNotification is that this method allows to send notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String}: name of notificationparams
{Array|Object}: notifications parameters. If object passed notifications will be delivered in a by-name fashion.
Subscribe to notification(s)
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Subscribe to internal notification(s) The only difference between this method and onNotification is that this method allows to subscribe to notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Subscribe to given notification(s) once.
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Subscribe to internal notification(s) once. The only difference between this method and onceNotification is that this method allows to subscribe to notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Unsubscribe from notification(s)
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Unsubscribe from internal notification(s) The only difference between this method and offNotification is that this method allows to unsubscribe from notifications with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String|Object}: name of notification or mapping object which have notifications names as keys and notifications handlers as values.handler
{function}: notification handler (should be passed only ifname
defined as string)
Registers a method(s) with given name and handler
Parameters:
name
{String|Object}: name of method or mapping object which have methods names as keys and methods handlers as values.handler
{function}: method handler (should be passed only ifname
defined as string)
Registers an internal method(s) with given name and handler The only difference between this method and registerMethod is that this method allows to register methods with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String|Object}: name of method or mapping object which have methods names as keys and methods handlers as values.handler
{function}: method handler (should be passed only ifname
defined as string)
Unregisters a method(s) with given names
Parameters:
name
{String|Array}: name of method or array of methods.
Unregisters an internal method(s) with given names The only difference between this method and unregisterMethod is that this method allows to unregister methods with prefix "rpc." and adds this prefix if it's missing.
Parameters:
name
{String|Array}: name of method or array of methods.
Returns list of registered methods names (not including internal methods)
Returns list of registered internal methods names (methods with "rpc." prefix)
Returns instance of connected JSON RPC socket by its ID
Parameters:
id
{String|Number}: ID of connected socket
Returns list of connected clients
Closes all connections related to namespace and removes this namespace
All connected sockets is wrapping in RPCSocket class, that provides basic RPC operations like call methods, send notifications etc.
Returns ID of connected socket
Returns native websocket object
Closes a WebSocket connection gracefully.
Parameters:
code
{Number}: Socket close code. Defaults to 1000.data
{String}: Optional data to be sent to socket before closing.
Calls a registered RPC method on server. Resolves once the response is ready. Throws if an RPC error was received.
Parameters:
method
{String}: An RPC method name to run on server-side.params
{Object|Array}: Optional parameter(s) to be sent along the request.timeout
{Number}: Optional RPC reply timeout in milliseconds.ws_options
{Object}: Optional parameters passed to ws. Not available on web browsers.compress
{Boolean}: Specifies whether data should be compressed or not. Defaults to true when permessage-deflate is enabled.binary
{Boolean}: Specifies whether data should be sent as a binary or not. Default is autodetected.mask
{Boolean} Specifies whether data should be masked or not. Defaults to true when websocket is not a server client.fin
{Boolean} Specifies whether data is the last fragment of a message or not. Defaults to true.
Calls an internal RPC method on server. The only difference between this method and callMethod is that this method allows to call methods with prefix "rpc." and adds this prefix if it's missing.
Parameters: See parameters of callMethod
Resolves with list of remote methods (array of strings) Rejects if RPC error was received
Resolves with list of remote events (array of strings) Rejects if RPC error was received
Sends a JSON-RPC 2.0 notification to server.
Parameters:
method
{String}: An RPC notification name to run on server-side.params
{Object|Array}: Optional parameter(s) to be sent along the notification.
Sends an internal RPC notification to server. The only difference between this method and sendNotification is that this method allows to send notifications with names with prefix "rpc." and adds this prefix if it's missing.
Parameters: See parameters of sendNotification