D
D
Dmitry2016-08-30 18:08:10
Socket.io
Dmitry, 2016-08-30 18:08:10

How to implement receiving new messages when connecting to WebSocket?

There is a project:
- Laravel 5.2
- Node JS
- Socket.IO 1.4.5

Socket Server:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();

redis.psubscribe('*', function(err, count) {
});
redis.on('pmessage', function(pattern, channel, message) {
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});
http.listen(9543, function(){
    console.log('Listening on Port 9543');
});


All this in conjunction with Events ( Laravel ).

When a message is created, event is called and the client connected to the socket receives the content, everything works as it should.

The question is how to implement a kind of "queue" of messages so that when connecting to the socket, the client receives all previous messages that should have come through the socket?

Or at least how to call an event (Laravel Event) when connecting to a socket, or in any other way access the backend (Laravel) and get certain data from it?

It is important to receive messages exactly when connecting / reconnecting to the socket.

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2016-08-30
@another_dream

As far as I remember, there is no such thing out of the box.
In this case, I would do this:
- duplicate the event in the same radish, mark it as "unread"
- when a message is received on the client, send a return request through the socket marked "read", and kill that key in the radish via nodejs
- when reconnecting, pick up all "unread" messages
Look, there may be other message queue implementations that support this mechanism out of the box.

D
deadmemoras, 2016-08-30
@deadmemoras

Ps: completely off topic (practically).
In general, I am also writing my own project, there will be dialogs (on a separate page, just routes).
Which is better:
ajax or reading about Node and on it?
ps: whole project in php + jquery

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question