New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc.Dial not supported in gopherjs? #838

Closed
pratapsu opened this Issue Jul 2, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@pratapsu

pratapsu commented Jul 2, 2018

The application I'm writing has to call into another go process via rpc to retrieve some data and show it on the web page.

Here is the code:

package main

import (
	"github.com/gopherjs/vecty"

	"fmt"
	"net/rpc"
	"os/exec"
	"time"

	"mm"
	"mm/mina/components"
	"mm/mina/store"
)


var (
	localServer *rpc.Client
	err         error
)


func main() {
	store.Init()
	vecty.SetTitle("Mina")
	body := components.Body{}
	vecty.RenderBody(&body)

	connectToServer()
}

func connectToServer() {
	// Is there a server already running?
	if localServer, err = rpc.Dial("tcp", ":29400"); err != nil {
		fmt.Println("No local server running.")
		fmt.Println("I will attempt to start a fresh one now.")

		// Start a fresh server, and connect to it.
		exec.Command("mms", []string{}...).Start()
		time.Sleep(2 * time.Second) // Why? Is the exec a goroutine?

		if localServer, err = rpc.Dial("tcp", ":29400"); err != nil {
			panic("Cannot start server: ")
		}
	}

	// Connected!
	fmt.Println("Connected to local server")
	var in int
	var out mm.T3
	if err = localServer.Call("MMSProtocol.Start", in, &out); err != nil {
		panic("Cannot initialize database: ")
	}
}

When i invoke "gopherjs serve" I get:

Uncaught Error: network access is not supported by GopherJS

What are my options?

Regards,

Pratap

@dmitshur dmitshur added the question label Jul 6, 2018

@dmitshur

This comment has been minimized.

Member

dmitshur commented Jul 6, 2018

Browsers don't provide access to raw TCP sockets, which net/rpc uses, so it can't work. This is why the compatibility table says "no net" for that package.

What are my options?

You can use the WebSocket API to create TCP-like full-duplex connection, which browsers do support, and base your RPC on top of that.

For example, see the github.com/gopherjs/websocket package and its websocket.Dial method. It gives you a net.Conn. Then you can create a new rpc.NewClient with that connection:

conn, err := websocket.Dial("ws://localhost:8080/rpc-websocket")
if err != nil {
	return err
}

client := rpc.NewClient(conn)
// Use client...

See shurcooL/play@e7e8936#commitcomment-9977203 for a full example of that being done. (You can also see shurcooL/play@91d89be#commitcomment-9976862 for an example of the same thing but with net/rpc/jsonrpc package.)

@pratapsu

This comment has been minimized.

pratapsu commented Jul 7, 2018

Thanks, @shurcooL, your suggestion worked perfectly for me.

@pratapsu pratapsu closed this Jul 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment