Beyn Technology
Published in

Beyn Technology

Working On The Multiple Cores With “Socket.IO”

Socket.io module has got good performance and offer a lot of features and ease. It can use at the Javascript web pages, at the NodeJS, at the ElectronJs, at the React, at the React-Native, and etc platforms. That’s why, socket.io is good choose for real time web/desktop/mobile applications.

Node.js works on the single thread. So we have got 4,8,16 etc core, but we can use only 1 core as default. This situation occurred from non-blocking structure of Node.js. While we works on big data, this situation can be big problem for us and we can be need to multiple cores.

Let’s Use A Common Session
We need to assign a common session between multiple cores. We will use sticky-session module at this article.

That basic template can use for both socket.io, express and sticky-session.

Sticky session module assign a session through IP address. Sessions distribute via Redis Adapter and all cores make communication on this structure. Just like multiple servers connecting to single mysql server.

Let’s Use An Adapter:

First we must make install and run Redis. After we should install and use “socket.io-redis” adapter. We can use different adapters; but we will use Redis Adapter in this article. This adapter will provide to work between all cores through Redis. First we must make install and run Redis.

Let’s Install Module:
npm i socket.io-redis | yarn add socket.io-redis

If you want define to number of core, you can add this parameter:

Each process is running on the different cores and connecting to same room as like below.

Different Cores In The Same Room

  • Used a vpn/proxy for different ip at the left screen, and it was assigned to “worker 3”
  • It was assigned to “worker 1” at the right screen.

All codes at here:

https://github.com/mhmttkmk0352/working-on-the-multiple-cores-with-socket.io

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store