socket, emit event to server from server

后端 未结 1 1165
旧巷少年郎
旧巷少年郎 2021-01-22 04:55

I have a server set up and client -> server events work great, server -> client events also work.

However, as I am writing to my DB I want to emit a server->server (or s

相关标签:
1条回答
  • 2021-01-22 05:30

    I understand where my logical misstep was at this point.

    Socket.io is (as far as I can tell) a robust server->client->server event handler, and is really great at that. However I was looking to handle some events within the app itself. I found what I was here in NodeJs' documentation.

    Changed code: I added:

    var EventEmitter = require("events").EventEmitter;
    var ee = new EventEmitter();
    

    and within my socket event I have:

    if (rows[0].rows === 0) {
        console.log('gonna emit');
        ee.emit('internal event', {'data': true})
        return;
      }
    

    I then created a new section in my server for internal events, and that has:

    ee.on("internal event", function (data) {
      console.log(data)
    });
    

    Which is working great.

    TL;DR: Socket.io is awesome for server->client->server interactions. Node's built in event handler is great for internal events.

    0 讨论(0)
提交回复
热议问题