I'm new to Node.js and Angular, and I'm going to prove it with my question.
In the Angular tutorials I've consumed, I've seen the "back-end" piece implemented with an Angular "service", created via e.g. "ng c s myservice", where myservice becomes an injectable that is added to the app's providers list. Most of the examples I've seen either implement this service with a local stub of test data, or via http requests to something like a Mongo database.
My (Angular) application, however, is required to connect to a TCP server on its back-end. Using the http protocol is not an option.
The first thing I tried was socket.io-client, only to find out it's http only. I then tried node.js's Net class, like this (with a trivial TCP service listening on the given port):
import { Injectable } from '@angular/core';
@Injectable()
export class RemoteService {
net = require('net');
client;
serverStatus: string = 'Unknown';
PORT: number = 51101;
IP = '127.0.0.1';
constructor() {
console.log('Initializing remote client. Calling ' + this.IP + ':' + this.PORT + ' ...');
this.client = this.net.createConnection({ port: this.PORT }, { host : this.IP }, () => {
console.log('Connected to server!!!');
});
}
}
... but this results in this error in the browser's console:
ERROR TypeError: this.net.createConnection is not a function
When I research that particular error, the common explanation (short version) is "You can't do that from a browser." (One example here.)
I do grok why you can't do TCP or UDP or similar from "the browser". What I'm not getting is how the piece that is my back-end qualifies as "the browser". Can anyone clarify this for me? (Am I misunderstanding what Angular is capable of doing?)
Is it at all possible to use Node.js's Net class in an Angular service, and if so, how?