ProjectOpenSea / stream-js

A TypeScript SDK to receive pushed updates from OpenSea over websocket.
https://docs.opensea.io/reference/stream-api-overview
MIT License
165 stars 52 forks source link

phonexi error accessing sessionStorage in node #349

Closed as-iotex closed 8 months ago

as-iotex commented 9 months ago

I am using NodeJS 18.18.0 on Ubuntu and I am getting an error because phoenix is accessing sessionStorage:

node_modules/phoenix/priv/static/phoenix.cjs.js:1031
    this.sessionStore = opts.sessionStorage || global.sessionStorage;
                                                      ^

TypeError: Cannot read properties of undefined (reading 'sessionStorage')
    at new Socket (/node_modules/phoenix/priv/static/phoenix.cjs.js:1031:55)
    at new e (node_modules/@opensea/stream-js/dist/index.cjs:1:5111)
    at Object.<anonymous> (/opensea-stream.js:8:16)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
    at node:internal/main/run_main_module:23:47

My script is as follows:

const { OpenSeaStreamClient } = require('@opensea/stream-js');
const { EventType } = require('opensea-js');
const { WebSocket } = require('ws');

const client = new OpenSeaStreamClient({
    token: apiKey,
    connectOptions: {
        transport: WebSocket
    }
});

client.onEvents(
    slug,
    [
        EventType.ITEM_RECEIVED_OFFER,
        EventType.ITEM_RECEIVED_BID,
        EventType.ITEM_TRANSFERRED,
        EventType.ITEM_LISTED,
        EventType.ITEM_CANCELLED,
        EventType.ITEM_METADATA_UPDATED,
        EventType.ITEM_CREATED,
        EventType.ITEM_SOLD,
    ],
    (event) => {
        // handle event
        console.log(event);
    }
);

I am not an expert in JS so please forgive me is this is a dumb question

Thanks

ryanio commented 9 months ago

i haven't run into this error before, but you can try adding https://www.npmjs.com/package/node-sessionstorage to your project, importing with const storage = require('node-sessionstorage') and providing { sessionStorage: storage } to phoenix

imduchuyyy commented 9 months ago

Same issue here

mrodriguez-bc commented 8 months ago

same issue here

sulyak commented 8 months ago

I also had this issue. I solved by removing @types/phoenix and installing phoenix@1.6

ryanio commented 8 months ago

I just tried this and it works for me, including in a PR to add to README, let me know if it works for you:

  • Install required libraries: npm install ws node-localstorage

    • Import and include the libraries in connectOptions:
    
    import { WebSocket } from 'ws';
    import { LocalStorage } from 'node-localstorage';
    
    const client = new OpenSeaStreamClient({
      network: Network.TESTNET,
      token: 'YOUR_API_KEY',
      connectOptions: {
        transport: WebSocket,
        sessionStorage: LocalStorage
      }
    });