Socket
Socket
Sign inDemoInstall

@pryv/socket.io

Package Overview
Dependencies
12
Maintainers
3
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @pryv/socket.io

Extends `pryv` with Socket.IO transport


Version published
Weekly downloads
1
decreased by-80%
Maintainers
3
Install size
2.30 MB
Created
Weekly downloads
 

Readme

Source

Socket.IO add-on for pryv

Extends the Pryv JavaScript library with Socket.IO transport and notifications.

Usage

The add-on extends pryv.Connection instances with a socket property.

Importing

NPM

npm install --save pryv @pryv/socket.io, then in your code (the add-on must be loaded once only):

const pryv = require('pryv');
require('@pryv/socket.io')(pryv);
<script> tag

pryv-socket.io.js must be loaded after pryv.js:

<script src="https://api.pryv.com/lib-js/pryv.js"></script>
<script src="https://api.pryv.com/lib-js/pryv-socket.io.js"></script>

Other distributions available:

  • ES6 version: https://api.pryv.com/lib-js-socket.io/pryv-socket.io-es6.js
  • pryv library bundled with Socket.IO and Monitor add-ons: https://api.pryv.com/lib-js/pryv-socket.io-monitor.js.

Using connection.socket

Once the add-on is loaded, pryv.Connection instances expose the socket property.

  • connection.socket.open() (asynchronous) opens the Socket.IO connection. It throws errors on failure.
  • connection.socket.api() is identical to Connection.api() but using the Socket.IO transport (see the library README)
  • connection.socket.on({event-name}, callback) registers an event listener. Possible event names are:
    • eventsChanged: when one or multiples events are deleted, changed or added.
    • streamsChanged: when one or multiples streams are deleted, changed or added.
    • accessChanged: when an access is deleted or added.
    • error: on error. The callback will receive the error as first argument.

Examples

Node.js
const pryv = require('pryv');
require('@pryv/socket.io')(pryv);

const apiEndpoint = 'https://{token}@my-computer.backloop.dev:4443/{username}/';
(async () => {
  const conn = new pryv.Connection(apiEndpoint);
  try {
    await conn.socket.open();
    conn.socket.on('eventsChanged', async () => {
      const res = await conn.socket.api([{method: 'events.get', params: {limit: 2}}]);
    	console.log('Last 2 events: ', JSON.stringify(res, null, 2));
    });
  } catch (e) {
    console.log('An error occured: ', e.message);
  }
})();
Browser
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
<script src="https://api.pryv.com/lib-js-socket.io/pryv-socket.io.js"></script>

<script>
const apiEndpoint = 'https://{token}@my-computer.backloop.dev:4443/{username}/';
(async function () { 
  try {
    await conn.socket.open();
    conn.socket.on('eventsChanged', async () => {
      const res = await conn.socket.api([{method: 'events.get', params: {limit: 2}}]);
    	console.log('Last 2 events: ', JSON.stringify(res, null, 2));
    });
  } catch (e) {
    console.log('An error occured: ', e.message);
  }
})();
</script>
Example web app

See here for a simple app that allows to log in a Pryv.io platform, register to monitor events changes and create notes. You can try it running there.

Contributing

See the Pryv JavaScript library README

License

BSD-3-Clause

Keywords

FAQs

Last updated on 27 Feb 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc