Implementing custom servers and sockets

Hello! This is my first post here, loving this framework so far.

I am trying to adapt an application I made to use Redwood, but I’m having a hard time with the backend part. Maybe I missed something from the tutorial or the reference, but I explain my case.

I previously had built a server in form of a class, which contained an Express server and a net.Socket() instance to do some things. Then it contained some HTTP routes (some GET, some POST, some PUT and a couple of DELETE). The routes touched some controllers which then touched a MongoDB instance.

I already changed my schema.prisma and my general database design to be based on PostgreSQL; I am having trouble orienting myself with the next steps: where should I put the server instance? The controllers should become all mutation resolvers? How do I handle the endpoints?

This is really the only big obstacle I’m encountering, since the web part is fairly simple. Any help on this would be hugely appreciated!

Hi. Glad you are enjoying the framework!

Have you done the Tutorial?

The Redwood api out of the box uses GraphQL so there aren’t necessarily those same REST endpoints like you’ve used before.

There are ways to build and use a REST api but I’d suggest getting familiar with GraphQlL and cell based data fetching first.

This may change in future releases, but for now GraphQL is the primary way to fetch data and the generators will build for that.

Hello, thank you for your answer!

Yes, I got that feeling and I am adapting all the workflow to use GraphQL; operations done by controllers I am entering in the mindset that will have to be done by the cells instead. The only thing I am not really understanding how to place is a websocket. To be clear, I need the backend to run something like this:

import net from 'net'

class SocketServer {
  private socket: net.Socket

  constructor() {
    this.socket = new net.Socket()
  }

  connect() {
    this.socket.connect({
      host: 'localhost',
      port: 3000,
    })

    this.socket.on('connect', () => {
      console.log('Connected to server')
    })

    this.socket.on('error', () => {
      console.log('Net socket error')
    })

    this.socket.on('close', () => {
      console.log('Closing socket...')
    })

    this.socket.on('data', (data) => {
      console.log(data.toString())
    })
  }
}

export default SocketServer

Also, another point I don’t quite have clear is: what happens if I change the model, but already defined the SDL and services? Is there a generator to update them or should I just delete them and run yarn rw g sdl Model again?

How do you intend to deploy?

We’re currently doing some work for v6 to make it easier to run and configure Redwood in a full Fastify server deploy, because you need a server to do web sockets. But that’s not yet available in a release.

What functionality are you providing over web socket?

Yes, I have to deploy the application on a real workstation. The point of the websocket is that the rest of the system is attached to a middleware which dispatches messages; these messages have to be caught by the socket (when they are intended for it, of course). This middleware is a separate application, so as far as I’m concerned is a black box which dispatches a certain defined set of JSON messages.

For deployment, I was also considering dockerizing the production build, to be honest. It is not an application that is meant to go on the web, so I don’t need a serverless solution. But I do have to figure out how open up a socket between the Redwood application and the middleware

There is an effort to containerize and as part of that have a traditional server where you can register Fastify plugins use websockets.

This is an experiment and is getting into a canary release soon. Well it’s in there now but not yet communicated how to use yet.

@dom is coordinating that effort.

Perhaps your use case will be a good test for this upcoming feature?

I think it would, yes. The only problem is that I’m in a bit of a hurry, so I’m also looking for a patchy brutal solution, if there is one. I mean, can I not instruct the backend to run just one other agent?

You can try this earlier experiment WebSockets in RedwoodJS

It won’t be how Redwood will use serverful moving forward but may get you started.

1 Like

Thanks a lot! I will check it out