0

My client wants to have an OPs Engineer run the Node Red side of the house, but all operations support staff are to use a Single Page App (And never ever touch Node Red), so Part of me was thinking that a sample uibuilder page will create a socket, but if i have an angular app exist and that socket too exists, I can get all the data I need without hacking together code. As they keep extending the use cases, the ui builder is switching from simple screen components to lists, forms, loaders, etc in 1 endpoint, with layer, lots of inline classes, and no organization.

I have a sample UiBuilder for NodeRed called "launch" which when working through some samples has a socket I can listen to. I thought that it would be useful if i could create a different app, which also listens to this socket. For every Message recieved, if it doesnt exist in a list, it will add it to the list and hen from there we will start user stories.

I figured to implement socket in an Angular App, through a service I created. I created a simple client-server relationship with some demos found online, but I am now trying to link it to the Node Red uibuilder launch which exists.

I thought it was going to be as simple as updating the url or path but that seems to not work.

Here is a sample file I was working with:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import * as io from 'socket.io-client'
import { environment } from 'src/environments/environment';
import * as Rx from 'rxjs/Rx';
export class SocketioService {
  private _socket;
  constructor(){}
  setupSocketConnection(){ 
    this._socket = io.connect(environment.SOCKET_ENDPOINT);
    let observable = new Observable( observer => {
      this.socket.on('msg', (data:string) => {
        console.log("INFO DUMP", data);
        observer.next(data)
      })
      return () => { this.socket.disconnect(); }
    })
    
    let observer = {
      next: (data: Object) => {
        console.log("In Emit.")
        this.socket.emit('msg', JSON.stringify(data));
      }
    }
    return Rx.Subject.create(observer, observable);
  }
}

and my env variable was: https://localhost:1800/uibuilder/vendor/socket.io/ because when looking up the network requests to the sample /launch i created, it looked like it was referencing here.

This is the demo i was using. https://tutorialedge.net/typescript/angular/angular-socket-io-tutorial/ to create something with angular8.

One other thing i was also noticing is that intellisense seems to not like my implementation of the line: this._socket = io.connect(...); saying "An accessor cannot be declared in an ambient context." Specifically: "Property 'connect' does not exist on type 'typeof import(.....node_modules/socket.io-client/build/index'"

I was thinking there might be some sort of handshake I need to mange to verify the socket, but it seems to never get that far.

Personal Deets:

  • Angular 8
  • Packages:
  • rxjs
  • rsjx-compat
  • socket.io
  • socket.io-client
  • @types/socket.io-client

My real purpose here is to just see a sample socket console dump, so there is no real front end, just a default Angular App running on :4200.

I dont think that the app itself would work under the uibuilder folders, as much as would just be deployed elsewhere and establish this connection.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • Hi, author of uibuilder here. Not entirely clear what you are asking here. Can you jump on the Node-RED Discourse where we can discuss this - then I can provide an answer here for reference. In general, it would be theoretically possible to connect to uibuilder's websocket though probably not much value there. Also possible to use the uibuilderfe library from another app. – Julian Knight Feb 20 '21 at 12:03
  • no problem, i will hop on there – Fallenreaper Feb 24 '21 at 14:10
  • 1
    @JulianKnight I created apost on the discourse, https://discourse.nodered.org/t/how-do-i-create-an-angularts-app-which-can-listen-to-nr-sockets/41561 – Fallenreaper Feb 24 '21 at 14:29

1 Answers1

0

You can create an Angular SPA using uibuilder.

uibuilder will extend the Node-RED web service to serve up your SPA and any additional front-end libraries that you choose to install.

In addition, it creates a websocket connection between Node-RED and your SPA.

So you don't need any other external web or websocket resources, uibuilder manages all of that for you.


Having said that, it is possible to run a uibuilder front-end app from a different webserver if you really need to. It isn't particularly well tested and it does require you to add some configuration manually. In this case, uibuilder will still create the websocket connection and manage it for you.

Julian Knight
  • 4,716
  • 2
  • 29
  • 42
  • So essentially, I guess i would just take the resulting NG SPA and run a `ng build --prod` and then put that information into the uibuilder endpoint I want to access it. Do you happen to have any samples of this as well, a simple hello World? It felt kinda wishy washy personally since NG also involves routing so i was curious as to how it would be handled. – Fallenreaper Mar 02 '21 at 18:41
  • Sorry, I don't have an NG example because I dislike it and don't use it :-) But yes, that should work. Remember that at the moment, you have to restart node-red if moving from src to dist in a uibuilder instance. Something that I need to fix. NG should continue to route and do everything else you are used to. uibuilder tries to stay out of the way. Mainly providing you with a pre-configured, robust websocket I/F and serving up resources from a number of locations to make life easy. There is an example of using a webpack built dist version in the WIKI though which should help you. – Julian Knight Mar 05 '21 at 16:18
  • It is ok. I decided to just remove Node-Red from the equation. :) – Fallenreaper Mar 09 '21 at 04:13