A
A
Al2016-07-15 22:34:16
Node.js
Al, 2016-07-15 22:34:16

How to access socket without request?

Usually, when requesting, you can get an object with a socket and do something (I have express with socket.io). But if, for example, I have a cron job that runs once an hour and does something, then it wants to notify all connected clients (emit), but there was no request, and there is no way to get the socket. Using global variables is not an option at all, I think. When running the application this way - app.set('io', io); , settim socket where io contains the main events and socket client authorizations.
How can you solve this problem without using a request?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Дмитрий Вапельник, 2016-07-15
@Sanu0074

тогда нужно заводить отдельный сторедж для соединений и добавлять их туда при каждом подключении. а связывать крон-джобу с express-сервером нужно через какой-то интерфейс. мне в голову приходит RabbitMQ

Александр Аксентьев, 2016-07-15
@Sanasol

Крон должен не emit делать, а писать какие-то нотификации в базу или какие-то хранилище.
А сокет сервер соответственно проверять с каким-то интервалом новые оповещения.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question