D
D
Dmitry2016-01-04 21:09:04
Node.js
Dmitry, 2016-01-04 21:09:04

How to send message through socket.io outside of io.sockets.on('connection', function (client)?

there is an index.js file
in it, I listen to events from the asterisk, I use Nami.

var nami = new namiLib.Nami(namiConfig);
....
nami.on('namiEvent', function (event) {
//вот тут надо отправить сообщение о событии
})

The same file serves a small chat, on socket.io
io = require('socket.io');
....
io.sockets.on('connection', function (client) {
client.on('adduser', function (message) {
...
});
}

How do I use the existing io = require('socket.io'); send asterisk event message to everyone in chat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2016-01-07
@MarcusAurelius

var nami = new namiLib.Nami(namiConfig),
    io = require('socket.io'),
    clients = [];

nami.on('namiEvent', function (event) {
  clients.forEach(function(client) {
    client. // делаем что нужно с каждыйм клиентом
    // или фильтруем массив или выбираем одного
  });
})

io.sockets.on('connection', function (client) {
  client.on('adduser', function (message) {
    clients.push(client);
  });
});

Don't use socket.io at all, you don't need it for anything, use https://www.npmjs.com/package/websocket on the server and just var socket = new WebSocket("ws://domain.com: port/path");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question