Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rpc-websockets

Package Overview
Dependencies
Maintainers
1
Versions
129
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rpc-websockets

JSON-RPC 2.0 implementation over WebSockets for Node.js

  • 7.11.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
448K
increased by3.66%
Maintainers
1
Weekly downloads
 
Created

What is rpc-websockets?

The rpc-websockets package provides a simple way to implement JSON-RPC 2.0 over WebSockets. It allows for both client and server implementations, enabling remote procedure calls (RPC) and event-based communication between clients and servers.

What are rpc-websockets's main functionalities?

Creating a WebSocket Server

This code demonstrates how to create a WebSocket server using rpc-websockets. The server listens on port 8080 and registers a 'sum' method that takes an array of parameters and returns their sum.

const WebSocketServer = require('rpc-websockets').Server;
const server = new WebSocketServer({ port: 8080 });

server.register('sum', (params) => {
  return params[0] + params[1];
});

console.log('WebSocket server is running on ws://localhost:8080');

Creating a WebSocket Client

This code demonstrates how to create a WebSocket client using rpc-websockets. The client connects to the server at ws://localhost:8080 and calls the 'sum' method with parameters 5 and 3, then logs the result.

const WebSocketClient = require('rpc-websockets').Client;
const client = new WebSocketClient('ws://localhost:8080');

client.on('open', () => {
  client.call('sum', [5, 3]).then((result) => {
    console.log('Sum:', result);
  }).catch((error) => {
    console.error('Error:', error);
  });
});

Subscribing to Events

This code demonstrates how to create a WebSocket server that emits events. The server defines an 'update' event and emits it every second with the current timestamp.

const WebSocketServer = require('rpc-websockets').Server;
const server = new WebSocketServer({ port: 8080 });

server.event('update');

setInterval(() => {
  server.emit('update', { timestamp: new Date() });
}, 1000);

console.log('WebSocket server is running on ws://localhost:8080');

Listening to Events on Client

This code demonstrates how to create a WebSocket client that listens to events. The client connects to the server at ws://localhost:8080, subscribes to the 'update' event, and logs any received updates.

const WebSocketClient = require('rpc-websockets').Client;
const client = new WebSocketClient('ws://localhost:8080');

client.on('open', () => {
  client.subscribe('update');
  client.on('update', (data) => {
    console.log('Update received:', data);
  });
});

Other packages similar to rpc-websockets

Keywords

FAQs

Package last updated on 03 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc