node-irc icon indicating copy to clipboard operation
node-irc copied to clipboard

Using node-irc in the client side with React

Open enriquemorenotent opened this issue 6 years ago • 2 comments

I am trying to build a IRC client using React, and I wanted to use this package. While running on the command line, a simple command like this seemed to work great.

new irc.Client( 'chat.freenode.net', 'Helloworld', { channels: ["#test"] })

But when I put the code inside my react app I meet this error in the console:

irc.js:796 Uncaught TypeError: net.createConnection is not a function
	at Client../node_modules/irc/lib/irc.js.Client.connect (irc.js:796)
	at new Client (irc.js:107)
	at reducer (reducer.js:50)
	at dispatch (redux.js:213)
	at redux-logger.js:1
	at Object.dispatch (index.js:11)
	at ChannelView.connectToServer (ChannelView.js:9)
	at onClick (ChannelView.js:24)
	at HTMLUnknownElement.callCallback (react-dom.development.js:100)
	at Object.invokeGuardedCallbackDev (react-dom.development.js:138)
	at Object.invokeGuardedCallback (react-dom.development.js:187)
	at Object.invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:201)
	at executeDispatch (react-dom.development.js:461)
	at executeDispatchesInOrder (react-dom.development.js:483)
	at executeDispatchesAndRelease (react-dom.development.js:581)
	at executeDispatchesAndReleaseTopLevel (react-dom.development.js:592)
	at forEachAccumulated (react-dom.development.js:562)
	at runEventsInBatch (react-dom.development.js:723)
	at runExtractedEventsInBatch (react-dom.development.js:732)
	at handleTopLevel (react-dom.development.js:4472)
	at batchedUpdates$1 (react-dom.development.js:16537)
	at batchedUpdates (react-dom.development.js:2131)
	at dispatchEvent (react-dom.development.js:4551)
	at interactiveUpdates$1 (react-dom.development.js:16592)
	at interactiveUpdates (react-dom.development.js:2150)
	at dispatchInteractiveEvent (react-dom.development.js:4528)

Why am I meeting this error on the browser with the same code? Is node-irc not suited to run on the browser? Do I need to create my own API to communicate with it?

enriquemorenotent avatar Jun 08 '18 14:06 enriquemorenotent

Is node-irc not suited to run on the browser?

No, it requires node apis.

timwangdev avatar Jun 28 '18 06:06 timwangdev

A late response, I'm looking into doing similar. In this case you'd need a NodeJS backend with a WebSocket.

Alternatively if you're using Electron you could either use preloads or give direct access to NodeJS within the BrowserWindow.

The net library that this library uses is a NodeJS Module thus doesn't exist within a browser context.

Hope this answers anyones questions about this in future, this also will apply to Angular or any other web based frontend.

tmfksoft avatar Aug 22 '19 15:08 tmfksoft