2

I tried using @types/socket.io-redis like so:

import { Server as HttpServer } from 'http';
import socketIo, { Socket } from 'socket.io';
import redis, { RedisAdapter } from 'socket.io-redis';

export default function setupWebsocket(server: HttpServer) {
    const io = socketIo().listen(server);
    io.adapter(redis(process.env.REDIS_URL));

    const adapter: RedisAdapter = io.of('/').adapter;    // Error here
}

At the section where the Error here comment is, I got a red underline at the adapter variable with the error:

Type 'Adapter' is not assignable to type 'RedisAdapter'.

Property 'uid' is missing in type 'Adapter'.

Can anyone help me fix this issue? I'm quite new to Typescript

4 Answers 4

3

This is correct behaviour, type of io.of('/').adapter is Adapter. Fact that you assigned specific implementation(RedisAdapter) of interface(Adapter) don't change property type, as later you might change to different implementation of Adapter.

Possible solution might be to assign adapter directly after creation

import { Server as HttpServer } from 'http';
import socketIo, { Socket } from 'socket.io';
import redis, { RedisAdapter } from 'socket.io-redis';

export default function setupWebsocket(server: HttpServer) {
    const io = socketIo().listen(server);
    const adapter: RedisAdapter = redis(process.env.REDIS_URL);
    io.adapter(adapter);
    //... more code here
}

Other solution is to cast to desired type

const adapter: RedisAdapter = io.of('/').adapter as RedisAdapter;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the delay. thank you very much for the answer and the explanation
1

Try casting using as:

const adapter: RedisAdapter = io.of('/').adapter as RedisAdapter;

Comments

1

I realize this is an old question... but I found this post before reading the docs :-)

According to the documentation for v6 of @socket.io/redis-adapter, you should import createAdapter function to initialize the adapter

https://github.com/socketio/socket.io-redis-adapter#typescript

import { Server } from 'socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { RedisClient } from 'redis';

const io = new Server(8080);
const pubClient = new RedisClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();

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

Comments

0

use this version socket.io @4.5.4,the issue is with the newer version of [email protected]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.