I'm trying to store some data in my local MongoDB instance using Go compiled to WebAssembly. The problem is, I cannot even connect to it. The mondog instance doesn't react to connection from wasm module in any way. This problem arises only when connecting from wasm module. The same code when compiled in ordinary way works fine, as well as connection from mongo shell. The runned mongod instance has no password protection.
My OS is Windows 10 in case that matters.
I've tried to change mongod bind_ip parameter from localhost to the actual local adress of my machine and use different browsers (Chrome 75.0.3770.80, Opera 60.0.3255.109).
Changing the timeout duration doesn't do the trick also.
func connectToMongo(URI string, timeout time.Duration) *mongo.Client {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(URI))
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatal(err) // It fails here
}
return client
}
func main() {
client := connectToMongo("mongodb://localhost:27017", 20*time.Second)
}
<html>
<head>
<script type="text/javascript" src="./wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch('main.wasm'),go.importObject).then( res=> {
go.run(res.instance)
})
</script>
</head>
</html>
I run mongod.exe without any parameters so it is binded to localhost.
I expected my code to connect to mongod instance, but actually I get the following error in browser console: "context deadline exceeded".
I'm still learning Go and a total newbie in JavaScript so I might be missing something very simple. Any help would be greatly appreciated.
You are trying to connect from WebAssembly to a local server, most likely using a protocol which isn't allowed from the browser WASM sandbox.
WebAssembly can't for instance open low-level network sockets out of the WASM sandbox, you're mainly constrained to the same things that you can do with JavaScript in terms of file, system and network access when you're running WASM in a browser.
It's worth reading up on the constraints that WebAssembly has around security and system access when used in a browser context as well as it's worth noting that it's not WebAssembly that's blocking your connection here, it's the browser that's running the WebAssembly.
Related
Was going to use Socket.IO for authorizing in a flutter application, but it wont work on anything other than localhost:
String serverURL = "https://example.awsapprunner.com";
IO.Socket socket = IO.io('$serverURL/client', <String, dynamic>{'transports': ['websocket']});
checkUserExists(String phoneNum, context) {
socket.connect();
socket.emit('login', phoneNum);
socket.on('login_error', (res)=> {
log("new user"),
});
socket.on('session-started',(sid)=>{
log("old user!"),
});
}
The app won't even connect to the socket so anything after socket.connent() never runs.
Here's what I have tried
'autoConnect': true
replacing 'https' with 'wss' or 'ws'
removing 'https://' altogether
Different Cloud Services (AWS, GC, Azure)
local server (works and connects perfectly fine)
connecting to the server via browser (also works perfectly fine)
Is it possible for a GRPC server written in golang to also handle REST requests?
I've found the grpc-gateway which enables turning an existing proto schema into a rest endpoint but I don't think that suits my needs.
I've written a GRPC server but I need to also serve webhook requests from an external service (like Github or Stripe). I'm thinking of writing a second REST based server to accept these webhooks (and possibly translate/forward them to the GRPC server) but that seems like a code-smell.
Ideally, I'd like for my GRPC server to also be able to, for example, handle REST requests at an endpoint like /webhook or /event but I'm not sure if that's possible and if it is how to configure it.
Looks like I asked my question before giving a large enough effort to resolve it my own. Here's an example of serving REST requests alongside GRPC requests
func main() {
lis, err := net.Listen("tcp", ":6789")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// here we register and HTTP server listening on port 6789
// note that we do this in with gofunc so that the rest of this main function can execute - this probably isn't ideal
http.HandleFunc("/event", Handle)
go http.Serve(lis, nil)
// now we set up GRPC
grpcServer := grpc.NewServer()
// this is a GRPC service defined in a proto file and then generated with protoc
pipelineServer := Server{}
pipeline.RegisterPipelinesServer(grpcServer, pipelineServer)
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %s", err)
}
}
func Handle(response http.ResponseWriter, request *http.Request) {
log.Infof("handling")
}
With the above, sending a POST to localhost:6789/event will cause the handling log line to be emitted.
I have hardly done anything with feathers sockets so far and therefore I need your help.
I have written a test which tests the functionality of my websocket to move users into the right channels. To do this I created two socketIo-clients in my test and let them connect to the websocket with the help of my own helpers, which also do the authentication when the connection is established.
If I run the test on its own, it will work fine. But once I run all the tests together, the test no longer works, because the SocketIo-Clients can not established any connection. This can be noticed, because no connection event is triggered at the server.
In my Test I do:
before((done) => {
server = app.listen(app.get('port'), done);
socketUrl = 'ws://localhost:5555}/';
});
socket = io(socketUrl);
and
after(async () => {
await server.close();
});
I found out, that any test, which is also doing server = app.listen(app.get('port'), done); before the socket test, will cause the socket test to fail. Can it be possible, that await server.close(); does not really close the http-server and/or the ws-server?
I am doing a project that is related with creating TCP/IP communication in Lua language. My computer is going to be a server and I wanna connect it with another computer.
So, here is the code:
local socket = require'socket'
local server = socket.tcp()
server:bind('*', 7200)
server:listen(32)
>>>>local client = server:accept()
--Here I have a problem. It is not working.
--It says:
--calling 'accept' on bad self (tcp{server} expected,got userdata in function)
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then
client:send('test') --end
-- done with client, close the object
client:close()
Where did I make a mistake?
Your code works: If I add an end at the bottom of your code it works for me.
I have a simple socket.io chat application which I've uploaded to one of the new Heroku 'cedar' stacks.
Now I almost have everything working but I've hit one stumbling block. On my localhost, I open a connection to the socket server from the client with:
// lots of HTML omitted
socket = new io.Socket('localhost', {port: 8888});
But on Heroku, I obviously must substitute something else in for these values.
I can get the port from the process object on the server like so:
port = process.env.PORT || 8888
and pass that to the view.
But what do I substitute for 'localhost'?
The correct way according the article on heroku is:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
socket = new io.Socket();
This ensures that io.Socket won't try to use WebSockets.
I was able to get Socket.IO v0.8 to work on Heroku Cedar by doing the following:
Within the Express app (in CoffeeScript in my case):
app = express.createServer();
socket = require("socket.io")
...
io = socket.listen(app);
io.configure () ->
io.set("transports", ["xhr-polling"])
io.set("polling duration", 10)
io.sockets.on('connection', (socket) ->
socket.on('myaction', (data) ->
...
socket.emit('result', {myData: data})
### The port setting is needed by Heroku or your app won't start
port = process.env.PORT || 3000;
app.listen(port);
And within the front-facing Javascript of your application:
var socket = io.connect(window.location.hostname);
function sendSocketRequest() {
socket.emit('myaction', $("#some_field").val());
}
socket.on('result', function(data) {
console.log(data);
}
Helpful links:
Heroku Node help
Heroku Socket.IO help
This has now changed as of Oct 2013, heroku have added websocket support:
https://devcenter.heroku.com/articles/node-websockets
Use:
heroku labs:enable websockets
To enable websockets and dont forget to remove:
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
After trying every combination under the sun I finally just left it blank. Lo and behold that works perfectly. You don't even need the port.
socket = new io.Socket();
I was also having this problem on heroku. I was able to make it work using the hostname "myapp.herokuapp.com" (or simply window.location.hostname, to work both local and in production) and setting the port to 80. I'm using SocketIO 0.6.0.
Wouldn't you just put your actual hostname?
2011-06-25T21:41:31+00:00 heroku[router]: Error H13 (Connection closed without response) -> GET appxxxx.herokuapp.com/socket.io/1/websocket/4fd434d5caad5028b1af690599f4ca8e dyno=web.1 queue= wait= service= status=503 bytes=
Does this maybe mean the heroku router infront of the app is not configured to handle web socket traffic?
[update]
It would appear as of 6/22/2011 the answer is yes... heroku does not support socket.io see this post: http://blog.heroku.com/archives/2011/6/22/the_new_heroku_2_node_js_new_http_routing_capabilities/