You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 line
1.7 KiB

  1. const { Server } = require("socket.io");
  2. const { get_conf, get_redis_subscriber } = require("../node_utils");
  3. const conf = get_conf();
  4. let io = new Server({
  5. cors: {
  6. // Should be fine since we are ensuring whether hostname and origin are same before adding setting listeners for s socket
  7. origin: true,
  8. credentials: true,
  9. },
  10. cleanupEmptyChildNamespaces: true,
  11. });
  12. // Multitenancy implementation.
  13. // allow arbitrary sitename as namespaces
  14. // namespaces get validated during authentication.
  15. const realtime = io.of(/^\/.*$/);
  16. // load and register middlewares
  17. const authenticate = require("./middlewares/authenticate");
  18. realtime.use(authenticate);
  19. // =======================
  20. // load and register handlers
  21. const xhiveframework_handlers = require("./handlers/xhiveframework_handlers");
  22. function on_connection(socket) {
  23. xhiveframework_handlers(realtime, socket);
  24. // ESBUild "open in editor" on error
  25. socket.on("open_in_editor", async (data) => {
  26. await subscriber.connect();
  27. subscriber.publish("open_in_editor", JSON.stringify(data));
  28. });
  29. }
  30. realtime.on("connection", on_connection);
  31. // =======================
  32. // Consume events sent from python via redis pub-sub channel.
  33. const subscriber = get_redis_subscriber();
  34. (async () => {
  35. await subscriber.connect();
  36. subscriber.subscribe("events", (message) => {
  37. message = JSON.parse(message);
  38. let namespace = "/" + message.namespace;
  39. if (message.room) {
  40. io.of(namespace).to(message.room).emit(message.event, message.message);
  41. } else {
  42. // publish to ALL sites only used for things like build event.
  43. realtime.emit(message.event, message.message);
  44. }
  45. });
  46. })();
  47. // =======================
  48. let port = conf.socketio_port;
  49. io.listen(port);
  50. console.log("Realtime service listening on: ", port);