How can i run a "Hello World" client from node?

#1

I am trying to figure out how this works. Is it possible to run the code inside script tags in the Hello-Example from node and not within the browser that is hosts it?
How this can be achieved that?
I copy the code in a nodeclient.js file, i installed the Resclient through npm in my test project and i replaced the “document.body” lines with console log as below

const resclient = require('resclient');
const ResClient = resclient.default;
let client = new ResClient('ws://localhost:8080');
client.get('example.model').then(model => {
    console.log(model.message);
    //document.body.textContent = model.message;
}).catch(err => {
    console.log("error-->", err);
    //document.body.textContent = "Error getting model. Are NATS Server and Resgate running?";
});

and the message after running this with node client.js is

error--> ResError {
  rid: 'example.model',
  method: 'subscribe.example.model',
  params: undefined,
  _code: 'system.unknownError',
  _message: 'WebSocket is not defined',
  _data: undefined }

while i have an open browser that responds with “Hello World” as expected

Thank you

#2

Welcome to the forum! :partying_face:

Unfortunately, ResClient currently runs only in the browser environment, and not under nodejs.

NodeJS doesn’t have a native WebSocket client, so one would need to rely on an external package like ws. And the ws package doesn’t have the same API as the browser native WebSocket object, so it is not a simple replacement.

That said, it shouldn’t be a big effort to allow the ResClient to take a “WebSocket factory” function as an alternative to the url parameter. The factory function would then be used instead of creating a native WebSocket object. Something like this in nodejs:

const ResClient = require('resclient').default;
 // This package should provide a similar API as the browser native WebSocket.
const WebSocket = require('isomorphic-ws');

let client = new ResClient(() => new WebSocket('ws://localhost:8080', {
  origin: 'http://localhost:8080'
}));

This is obviously not implemented yet, but I think the idea should work for nodejs usage.

I added an issue about it: Support NodeJS · Issue #30 · resgateio/resclient · GitHub

Best regards,
Samuel

#3

Thank you for your response.
It answers my question why it didn’t run because of node js doesn’t have websocket client.
Unfortunately don;t even your suggested workaround doesn’t work because resclient expects a string.
Looking and studying the docs and the examples, is resclient needed from a node client to connect with the resgate gataway to nats server?
A node client can’t be connected directly with the nats server having the same functionality?

Thank you

#4

Sorry for causing a misunderstanding: My code snippet was not a workaround, it was a suggestion on a new feature to add to ResClient. The feature must first be created, and a new version published to npm, before you can use it.

It is usually only the client application (the browser web application, or mobile app) that accesses Resgate with WebSocket (using ResClient), or with HTTP.
Services hosted on the server usually connect directly to NATS server, as shown in the below illustration where two service, NodeJS and Go, connects directly to NATS, and not to Resgate:

RES Network

If a service (eg. the NodeJS service) wish to access real-time data from another service (eg. the Go service), this can be done over NATS. But, there is currently no library for NodeJS that helps you with that. You would have to manually send get requests and listen for event messages directly over NATS.

There are plans to simplify inter-service communication using the RES protocol by extending the service libraries, but the first language to get that support will be the go-res package for Go.

Hmmm. Hope I didn’t confuse things more now :slight_smile:

Best regards,
Samuel