Maciej Cieślar
Feb 13, 2020 ⋅ 14 min read

Scalable WebSockets with NestJS and Redis

Maciej Cieślar A JavaScript developer and a blogger at mcieslar.com.

Recent posts:

Understanding Solid Js Props A Complete Guide From Beginner To Advanced

Understanding SolidJS props: A complete guide

Let’s see how SolidJS props work to promote component reusability, exploring basic to advanced concepts for a complete understanding.

Temitope Oyedele
Dec 7, 2023 ⋅ 11 min read
Eleventy Vs. Next.js Static-Site Generation

Eleventy vs. Next.js for static site generation

We evaluate Eleventy and Next.js and compare both static site generators in terms of performance, developer experience, scalability, and ecosystem.

Nelson Michael
Dec 7, 2023 ⋅ 11 min read
Build Full-Stack App React Goxygen

Build a full-stack app with React and Goxygen

We show how to use Goxgen to scaffold a full-stack React app. See how to integrate React with Go and modify Goxygen to suit your project requirements.

Clara Ekekenta
Dec 6, 2023 ⋅ 8 min read
Express Js Adoption Guide Overview Examples Alternatives

Express.js adoption guide: Overview, examples, and alternatives

Express.js is a Node.js framework for creating maintainable and fast backend web applications in JavaScript. In the fast-paced world of […]

Antonello Zanini
Dec 6, 2023 ⋅ 17 min read
View all posts

12 Replies to "Scalable WebSockets with NestJS and Redis"

  1. I’m not sure the interface AuthenticatedSocket was ever mentioned. Is this the correct typing?

    interface AuthenticatedSocket extends Socket {
    auth: {
    userId: string,

  2. Why implement your own solution over using socket.io-redis? i havn’t actually used socket.io-redis (currently trying to figure out the best solution) but it seems it abstracts the “socket state” so you don’t have to write your own implementation?

  3. socket.io-redis requires sticky session which is not available everywhere. Plus this way we got more control over how the connections are stored and used.

  4. Please I’m dealing with this now. How then can this be solved using s SPA like angular. I’m not aware that I can use sticky sessions with angular 8 but what are your thoughts? Can this solution work with jwt?

  5. Hello
    How I can use this websocketgateway in conjunction with graphql subscription?
    I can not find any tutorial that make relation between nestjs, graphql and socket.io

  6. I attempted to enable the swagger plugin but it is causing issues with the custom redis provider.


    __metadata(“design:paramtypes”, [typeof (_a = typeof redis_providers_1.RedisClient !== “undefined” && redis_providers_1.RedisClient) === “function” ? _a : Object, typeof (_b = typeof redis_providers_1.RedisClient !== “undefined” && redis_providers_1.RedisClient) === “function” ? _b : Object])

    ReferenceError: redis_providers_1 is not defined
    at Object. (/usr/src/app/api/dist/main.js:2685:58)
    at __webpack_require__ (/usr/src/app/api/dist/main.js:749:30)
    at fn (/usr/src/app/api/dist/main.js:60:20)
    at Object. (/usr/src/app/api/dist/main.js:2566:25)
    at __webpack_require__ (/usr/src/app/api/dist/main.js:749:30)
    at fn (/usr/src/app/api/dist/main.js:60:20)
    at Object. (/usr/src/app/api/dist/main.js:2533:36)
    at __webpack_require__ (/usr/src/app/api/dist/main.js:749:30)
    at fn (/usr/src/app/api/dist/main.js:60:20)
    at Object. (/usr/src/app/api/dist/main.js:2503:35)

  7. Congratulations on the content, I am learning a lot. Just a note, if you use the emitToAuthenticated method elsewhere in the code, even those who are not authenticated have access to messages sent through the socket. How to fix this?

  8. Hello, great article. I implemented your code and it works fine when I import the SharedModule and use the RedisPropagatorService from one of my application service. However when I do the same from another application service I get the following error:

    [Nest] 29876 – 07/28/2021, 11:02:25 AM ERROR [ExceptionHandler] A circular dependency has been detected. Please, make sure that each side of a bidirectional relationships are decorated with “forwardRef()”.
    Error: A circular dependency has been detected. Please, make sure that each side of a bidirectional relationships are decorated with “forwardRef()”.

    This is happening when RedisModule gets instantiated and the RedisService has an undefined value during the NestJS insertProvider call during app creation.

    There are no conflicting references that should cause any circular references between these services and I am puzzled how come it works fine from one service and not from the other.

    Any help would be much appreciated.

  9. Thanks for the great tutorial.

    I had an issue while testing my application after adding Redis as described here. The problem was that the connections to Redis are never closed, so jest never exits, breaking my CI pipeline. Adding a onApplicationShutdown function where we disconnect the redis clients did the tricks.

    export class RedisService implements OnApplicationShutdown {
    private readonly logger = new Logger(RedisService.name)

    private readonly redisSubscriberClient: RedisClient,
    private readonly redisPublisherClient: RedisClient,
    ) {}

    onApplicationShutdown() {

    @Majiec would you consider adding this to the tutorial, saving someone some time in the future? I would also argue it is good practice closing the connections on shutdown anyway.

Leave a Reply