heroiclabs / nakama-js

JavaScript client for Nakama server written in TypeScript.
https://heroiclabs.com/docs/nakama/client-libraries/javascript/
Apache License 2.0
194 stars 55 forks source link

Socket related Issue #205

Closed itsabubaker closed 1 week ago

itsabubaker commented 1 week ago

I am new to Nakama. And I am trying to send message in a group. My code is: exports.sendGroupMessage = async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); }

const session = req.session;
const { groupId, messageContent } = req.body;

try {

    // Create Nakama socket using WebSocket adapter
    const socket = client.createSocket();
    console.log(socket);
    const appearOnline = true;
    const socketSession = await socket.connect(session, appearOnline);
    console.log(socketSession);
    // Join the group chat before sending the message
    const result = await socket.joinChat(groupId, 3, true, false); // Type 3 is for group chat
    console.log("Joined chat:", result);

    if (result) {
        // Send the group message
        const messageAck = await socket.writeChatMessage(groupId, messageContent);
        return res.json({ messageAck });
    } else {
        return res.status(400).json({ errors: [{ msg: 'Failed to join chat' }] });
    }
} catch (error) {
    console.error('Failed to send group message:', error);
    const statusCode = error.status || 500;
    const statusText = error.statusText || 'Unknown error occurred';
    return res.status(statusCode).json({ errors: [{ msg: statusText }] });
}

};

But I am getting following error:

Failed to send group message: ReferenceError: WebSocket is not defined at WebSocketAdapterText.isOpen (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\node_modules\@heroiclabs\nakama-js\dist\nakama-js.cjs.js:3319:70) at _DefaultSocket.connect (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\node_modules\@heroiclabs\nakama-js\dist\nakama-js.cjs.js:3370:22) at exports.sendGroupMessage (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\controller\api\ChatController.js:130:38) at Layer.handle [as handle_request] (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\node_modules\express\lib\router\layer.js:95:5) at next (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\node_modules\express\lib\router\route.js:149:13) at middleware (D:\TechShield\Github\Nakama\NakamaApiRepo\NakamaApi\node_modules\express-validator\lib\middlewares\check.js:16:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

What problem is exactly? Can it be solved? Solution?
linear[bot] commented 1 week ago

SDK-856 Socket related Issue

mofirouz commented 1 week ago

If I'm reading the stack correctly, you are using this library as part of a NodeJS application. This library only supports Web and not NodeJS. You can add NodeJS by adding your NodeJS WebSocket adapter. Feel free to open a PR with these changes.

Also, for the future, development questions should be asked on https://forum.heroiclabs.com.

Thanks

itsabubaker commented 1 week ago

Can you share a relevant code snippet or tutorial for adding webSocket Adapter to use Nakama real time chat?

On Sun, Oct 20, 2024 at 6:07 PM Mo Firouz @.***> wrote:

Closed #205 https://github.com/heroiclabs/nakama-js/issues/205 as completed.

— Reply to this email directly, view it on GitHub https://github.com/heroiclabs/nakama-js/issues/205#event-14750706353, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4MTIGQZBZSVKSD3W5HJLRLZ4OTKZAVCNFSM6AAAAABQIPKYKSVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJUG42TANZQGYZTKMY . You are receiving this because you authored the thread.Message ID: @.***>

mofirouz commented 1 week ago

You can take a look at this adapter to add the NodeJS adapter.