bigstepinc / jsonrpc-bidirectional

Bidirectional RPC over WebSocket, Worker, WebRTC and HTTP with extensive plugin support.
MIT License
110 stars 22 forks source link

RPC Websocket: Bidirectional JSON-RPC Client on Server without registering endpoint? #47

Closed DrSensor closed 6 years ago

DrSensor commented 6 years ago

Hi, after reading Bidirectional JSON-RPC over WebSocket I had little confusion. Is it possible to serve JSON-RPC from nodejs without specifying endpoint functions? e.g no jsonrpcServer.registerEndpoint(new TestEndpoint())

I bump into a use case that the server (set as JSON-RPC client) doesn't know anything about the endpoint function (ping(str), add(numA, numB), etc) that the JSON-RPC server (run on Web Browser) has. For example, given this CLI:

$ rpc-call-to  demo/index.html  --add 1,2
return 3

In here rpc-call-to act as a websocket server and also rpc-client while demo/index.html contain TestEndpoint specified by the end-user.

oxygen commented 6 years ago

For the HTTP and WebSocket transports, the endpoint is just an http URL (prefixed with ws:// instead of http:// for websockets). WebSockets always run over HTTP at a specific URL pathname. For other transports the endpoint doesn't mean anything, or is just a way to categorize functions into some kind of namespaces.

You can have / as an URL path or /api or anything you need. See the HTTP server part where it decides that /api goes to JSONRPC.

To keep things simple, register just one endpoint at specific URL pathname, and use that as convention everywhere.