Necromancers Playing MTG

    @pryv/socket.io

    2.3.6 • Public • Published

    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.rec.la: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.rec.la: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

    Install

    npm i @pryv/socket.io

    DownloadsWeekly Downloads

    66

    Version

    2.3.6

    License

    BSD-3-Clause

    Unpacked Size

    13.9 kB

    Total Files

    6

    Last publish

    Collaborators

    • perki
    • kebetsi
    • sgoumaz