How do I make my Socket.io JavaScript codes to connect to OGS

I am not sure if anyone experimented with JavaScript for socket connection with OGS. I had some difficulties getting my codes to work.

const { io } = require("socket.io-client");
async function connectSocket(accessToken) {
	let url = 'https://online-go.com/socket.io/';
	params = {
		EIO: '4',
		transport: 'websocket'
	}
	header = {
		extraHeaders: {
			Authorization: `Bearer ${accessToken}`
		}
	}
	let socket = io(url, params, header);
	socket.on('connect', () => {
		console.log('socket connected');
	})
}

After I ran the code, the terminal just froze… It didn’t give me any response or printed any texts. Can you see anything wrong with my codes?

I am using this post API Python recap/ help as a reference. If possible @Dyonn do you have any insights?

1 Like

Hmm, I don’t think I know how to help, Other than to say that he server doesn’t seem to be good aty sending messages back, so maybe if you write the rest of the code, you’ll see that it has actualy worked, just not told you?

1 Like

Could you share the full repo on GitHub or something? This code snippet won’t output anything because you never call connectSocket(), but I’m guessing that was just omitted for brevity.

But other than that, I don’t see what the problem is. I wonder if the socket is already connected by the time you register the “connect” handler. So it doesn’t get emitted. @Dyonn idea of sending/receiving more messages seems good - there’s some ping/pong stuff in OGS code that you could try to emulate: online-go.com/sockets.ts at 09a424e7f2fe9bf8517cda0dfd7a2361313fedcb · online-go/online-go.com · GitHub

Okay here’s a pretty minimal working example of interacting with the OGS socket server via Node.js:

const { io } = require("socket.io-client");

let url = 'https://online-go.com';
params = {
    transports: ["websocket"],
}

let socket = io(url, params);

socket.on('connect', () => {
  console.log('socket connected');
  socket.emit("hostinfo");
})

socket.on("hostinfo", (hostinfo) => {
    console.log("Termination server", hostinfo);
}) 

output:

socket connected
Termination server {
  hostname: 'ogs-termserver-8689699b58-z9hhf',
  clients: 0,
  uptime: 20549.259,
  'ggs-version': 'rengo-1.2.2'
}

Note that in params, I set transports: ['websocket'] instead of transport: 'websocket' (see socket.io docs). Also the URL is just 'https://online-go.com'.

I wasn’t able to get the ping/pong to work, but I think that’s because I haven’t sent the "authenticate" message yet. You will likely need to figure out how that works in order to do anything useful. I think that’s where the accessToken gets sent, but I’ve never done it myself…

Thanks!! I was surprised that you don’t need the bearer token to establish the web socket. I will play with the code and work on the making move part.

1 Like

@benjito if I successfully create a demo board game with the real time API, will I be able to see it popped up like I would with the website clicking?

I think I’m having trouble understanding the question. You’re asking if the demo will be viewable in the browser? I think the answer would be yes. You can get the review id from the XHR response, or you can just go to your profile and look at the newest entry in the Reviews and Demos table.

But it kind of sounds like you’re expecting the script to generate a GUI, which definitely will not happen unless you’re doing something clever.

That makes sense. Thanks for the clarification!!

1 Like