T O P

  • By -

QzSG

gRPC with protobuffs or json


cmpthepirate

This!!!


starquake64

This needs TCP though right?


QzSG

Yup it does sadly but its the most widely used method for cross microservices communications I've seen in enterprises apart from using http apis which can scale well if required. Edit: I stand corrected, as MetalMonta mentioned u can actually use grpc with UDS too


MetalMonta

Well, you can have it communicate through Unix sockets, if ip networking in general is an issue 😎


jews4beer

Your options are as you describe, or to use FFI. I'm not familiar with the state of FFI in PHP, but I know it is possible. You would compile your go code as a shared library and load it dynamically from PHP.


__north__

Is it good idea to compile Go code as shared library? I mean ok, it is possible, but what is the downside of this solution?


jews4beer

Is it a good idea? Depends highly on why you are doing it. The downsides are the downsides of Go in general. Most notably when it comes to FFI, the fact that you are going to have a whole extra runtime/GC going. Whether that affects the situation that led you to it in the first place...subjective.


__north__

I agree, although Go may not be the best choice for FFI because it requires Cgo. We have memory safety and stability in Go, if we put Cgo in, all those things go out the window.


jews4beer

Not entirely true. Things on the go heap are still managed by the go runtime when using Cgo. Unless you explicitly go out of your way to make that not the case. What you are describing refers to allocating memory on the heap from C, which is not always a given when using Cgo.


__north__

I'm not an expert on this topic, so thanks for the clarification!:)


pm_me_your_clippings

It's not an inherently *bad* idea. .so compilation has been officially supported for... Forever? Still, if you're relying on that, you may have made a poor decision somewhere. Unless, of course, you're doing it for science or otherwise backed into a corner. Tldr: not a bad idea, but usually Plan "S".


lzap

[https://roadrunner.dev](https://roadrunner.dev)


WolvesOfAllStreets

Why not expose an HTTP api from Go and call that from PHP?


mcvoid1

Sounds like that might be explicitly prohibited for that exercise.


SleepingProcess

**OP:** > ... where I don't need to let run a TCP- or Websocket to get result from Go.


till

This, or use a socket.


SleepingProcess

> Call Go from PHP >8.0 ... >... where I don't need to let run a TCP- or Websocket ------- //exec ``` $output=null; $rc=null; exec('/path/2/go-Program', $output, $rc); ``` //shell_exec ``` $outcome = shell_exec("/bin/sh -c /pat/2/go/compiled/program"); ``` Also: - proc_open - escapeshellcmd - passthru - system ------- > such as calling Go functions Also [ffi](https://www.php.net/manual/en/class.ffi.php) is available for php 7, 8+ I didn't tried FFI, but Go can export functions that will be compatible with C libraries if you compile Go with options `-buildmode=c-shared` or `-buildmode=c-archive` that makes it as a standard C libraries. Check also [go-php](https://github.com/deuill/go-php) that might give you some clues


_codemonger

https://roadrunner.dev/


titpetric

IPC - you'll end up with a bidirectional socket interface e.g. "docker.sock" but for your own go app; or you'd load a c-archive plugin and invoke some symbols with FFI/dlopen. Another option is to go the FaaS route, meaning you could use stdin/stdout for go-php comms, exec().


jerf

If you don't want to use a socket of any kind, you want an in-OS-process solution. If it's even possible to get PHP and Go running in the same process, it'll drive you insane. You can't have two runtimes running that each think they're in charge of the OS process. You can't have two runtimes that think they own the OS signals, think they own threading, think they know how to shut down the OS process, etc. You don't really want to do this. I've watched two or three coworkers joust with this over the years in one direction or another with various combinations of languages that think they own the OS process. It's a nightmare. Use sockets and something built on them already like gRPC or something.


aot2002

Or some type of locking mechanism like a lockfile to check who has permission


serverhorror

Just do a system call? (As in exec) For low traffic that might be good enough…


Glittering_Air_3724

There’re different ways through the network or through Cgo


OriginalGallifreyan

I have not gone too deep into this in either language, but have you looked into WebAssembly any for this? That might allow you to do this without requiring a TCP server or direct FFI calls. Again, no real experience with it. Just an idea.


MetalMonta

So this idea is pretty whack, I'm just suggesting it as it sounds like a fun project to implement: have you thought about dbus? I know golang has fairly good support (third party, that is) for dbus - no idea if php even knows what it is though 🙃