Ryan H
Ryan H

Reputation: 2945

Can't connect to socket.io server from Node JS script

I've got a socket.io server running on Node JS listening for messages. I now need to start another Node JS script (my client) which will send messages to the server, that's at a specific URL and port.

I'm passing my URL into the connect function but am getting the error:

TypeError: io.connect is not a function

How can I pass my URL to connect to into this. My script is:

const io = require('socket.io');

const socket = io.connect('http://localhost:3000');
socket.on('connect', function () {
  socket.send('hello') // send to server from running JS script
});

Upvotes: 0

Views: 82

Answers (1)

jfriend00
jfriend00

Reputation: 707158

If you're attempting to initiate a socket.io connection from node.js, you need the client-side library which would be:

const io = require('socket.io-client')

And, of course, you have to install that library.

Upvotes: 1

Related Questions