Reputation: 6372
There is a wss on another server that I'd like to connect to. Let's say its address is: wss://123.123.123.12:8843
I have a nodejs/expressjs app. How do I connect to it using socket.io? I know how to implement socket.io for a server/client setup, but how do I do it for two servers (one of which I don't have access to).
How do I setup my expressjs app to connect to the wss? Is there a way using socket.io or do I need to do it another way?
Upvotes: 1
Views: 10659
Reputation: 91799
Socket.IO isn't a WebSocket. It is a transport system that supports WebSockets, but if you want to interface with other WebSockets, you'd have better luck using the ws module, which Socket.IO uses internally. This is how it's used:
var WebSocket = require('ws');
var ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function() {
ws.send('something');
});
ws.on('message', function(data, flags) {
// flags.binary will be set if a binary data is received
// flags.masked will be set if the data was masked
});
If you actually just want to communicate with another Socket.IO server, then install the client on the server. It's used the same way as you would use with a browser client.
npm install socket.io-client
And then usage:
var socket = require('socket.io')('http://host/path');
socket.on('connect', function() {
socket.on('event', function(data) { });
socket.on('disconnect', function() { });
});
Upvotes: 7