Skip to content

Error: The client is closed #435

@MessyComposer

Description

@MessyComposer

I'm having issues getting the adapter to work with multiple nodes.
When ever I emit something, it prompts a "UnhandledPromiseRejectionWarning: Error: The client is closed" error, and looking into redis-cli > PSUBSCRIBE *
I do not see channels like mentioned in the readme.

I managed to get rid of these errors by using pubClient.connect and subClient.connect, and the event is broadcasted to clients connected to same node, but not to ones in other nodes. And the channels are still empty, like before.

I've tried running redis server with docker and locally on osx and linux, with no difference.
If anyone could point me in the right direction as to how to solve this, that would be great.

My code is as follows

import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
import { createClient } from "redis";
import { createAdapter } from "@socket.io/redis-adapter";

const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });

const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();

io.adapter(createAdapter(pubClient, subClient));

io.on("connection", (socket) => {
  socket.emit('connected', 'hello')
  console.log(`Client: connected (${socket.id})`);
  
  socket.on("client:message", (message) => {
    socket.broadcast.emit('client:message', message);
  });
});

httpServer.listen(process.env.PORT || 3000);
app.get('/', function(req, res,next) {
    res.sendFile(__dirname + '/index.html');
});

index.html

<!doctype html>
<html lang="en">
    <head></head>
    <body>
      <div style="display:flex;flex-flow:column;width:200px;">
        <input value="Send Message" type="button" onclick="submitMessage();"/>
      </div>
      <script src="/socket.io/socket.io.js"></script>
      <script>
        var socket = io({
          transports: [ "websocket" ],
        });
        socket.on('client:message', data => console.log(data));
        const submitMessage = () => socket.emit('client:message', 'test message');
      </script>
    </body>
</html>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions