O
O
oowl2013-02-03 18:12:50
JavaScript
oowl, 2013-02-03 18:12:50

Getting data between page load and websocket connection initialization?

Friends, help expand your consciousness.

Let's assume that there is a stream of events occurring on the server and they need to be displayed on the client. Let it be a chat or something like that. There is an experience of using Dklab_realplexor to solve this problem. I'm looking at node.js + socket.io and I can't figure out this thing.

The algorithm for working with Dklab_realplexor is as follows:
1. Get the ID of the last event (cursor).
2. Received from the database a certain number of events with id <= cursor.
3. Render the page showing the latest events.
4. Initialize the connection to realplexor by telling it cursor.

As a result, we will not lose a single event. We will immediately receive all the events that happened between the request for old events and the connection to the realplexer by specifying the cursor.

How does this loss not happen (or happen) when using socket.io? Do I understand correctly that the only way to get around the problem is to create a “start” event on the client, passing the id of the last event, and the server to return everything that has happened since then?

CLIENT

var socket = io.connect('http://localhost');<br>
  socket.emit('start', { lastKnownId: lastKnownId });<br>
  socket.on('event', function (data) {<br>
    // в data новые события<br>
  });<br>


SERVER
var io = require('socket.io').listen(80);<br>
  io.sockets.on('connection', function (socket) {<br>
    socket.on('start', function (data) {<br>
      socket.emit('event', { data: getEventsAfter(data.lastKnownId) });<br>
    });<br>
  });<br>


And the second question is from here. How are network outages counted? Those. I will not receive events on the client that occurred while there was no network connection for a few seconds?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
esenin, 2013-02-04
@esenin

Return events that occurred after a certain label by ID. All right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question