Pubnub class not working in next.js server

184 Views Asked by At

I'm trying to use pubnub access manager with next.js, but the PubNub class works fine in the client side but when I try to run it next.js server it doesn't work!

api/pubnub/

export default async function handler(req, res) {
try {
    const authKey = generateAuthKey();
    const pubnub = new PubNub({
        publish_key: "pub-c-..",
        subscribe_key: "sub-c-..",
        secret_key: "sec-c-..",
        userId: "myUniqueUserId",
    });

    const token = await pubnub.grant(
        {
            channels: ["chats.room1", "chats.room2"],
            authKeys: [authKey],
            ttl: 1440,
            read: true,
            write: true,
        },
        function (status: any) {
            // console.log(status);
        }
    );
    res.status(200).json(authKey);
} catch (error) {
    console.log(error);
    return res.status(500).json(error);
}
}

The message Error I get: "500 (Internal Server Error)".

pubnub version: "^3.16.5"

Full Axios Error:

{
"message": "Request failed with status code 500",
"name": "AxiosError",
"config": {
    "transitional": {
        "silentJSONParsing": true,
        "forcedJSONParsing": true,
        "clarifyTimeoutError": false
    },
    "transformRequest": [
        null
    ],
    "transformResponse": [
        null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1,
    "env": {
        "FormData": null
    },
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json"
    },
    "method": "post",
    "url": "/api/pubnub/",
    "data": "{}"
},
"code": "ERR_BAD_RESPONSE",
"status": 500

}

This error from my console

error - TypeError: Cannot use 'in' operator to search for 'EventEmitter' in undefined
at keepAliveIsEmbedded (/node_modules/pubnub/node.js/lib/xdr.js:12:25)
at Object.createInstance (/node_modules/pubnub/node.js/lib/xdr.js:183:9)
at new CREATE_PUBNUB (/node_modules/pubnub/node.js/pubnub.js:69:25)
at runMicrotasks (<anonymous>)
0

There are 0 best solutions below