Guillaume Wuip
Guillaume Wuip

Reputation: 363

Can I use socket.io in my Chrome Packaged App?

I need a socket.io connexion between a Chrome Packaged App and a NodeJS script. I'm familiar with both NodeJS and socket.io but not with Chrome Packaged App.

It seems to be difficult (I haven't find any doc explaining how to do that). I first try to simply load the socket.io.js script served from my NodeJS (http://localhost:8080/socket.io/socket.io.js). But it's forbidden by the CSP.

Then I found the socket.io-client . I'm not very familiar with english but I understand that it deals with loading the socket.io client script without the server, right ?

So I try to load the script "socket.io-client/socket.io-client.js" in my background.html page but chrome send me an error :

Uncaught Error: failed to require "socket.io" from "root"

I'm lost ... Before trying to go further, I simply ask myself if socket.io is a good idea to establish a dialog between my Packaged App and Nodejs.

And if it is, how can I load and run it an the client side ?

Edit : I wonder if I have to use classic Websocket instead of socket.io. I know it works with Packaged App as shown here but it'll take more time to develop the same functionalities socket.io give us (passing objet).

Thank you for your help :)

Upvotes: 4

Views: 2028

Answers (2)

Marc Rochkind
Marc Rochkind

Reputation: 3740

Don't use socket.io in Chrome Apps for low-level socket communication. Use chrome.sockets.tcp or chrome.sockets.tcpServer.

WebSockets are a particular socket protocol, and the HTML5 WebSockets API works fine in Chrome Apps. (Note: WebSockets are not a way to access HTTP sites, but are a much newer protocol usable only with servers specifically programmed to handle WebSocket clients.)

For HTTP, you don't have to use chrome.sockets.tcp. You'll most likely find that good old XMLHttpRequest can do everything you want.

Upvotes: 2

Angular University
Angular University

Reputation: 43087

Try this, take the socket.io.js from node_modules:

$ find . -name socket.io.js
./node_modules/socket.io-client/dist/socket.io.js

Copy it together with the client side libs, and then include it like this:

<script type="text/javascript" src="js/libs/socket.io.js"></script> 

Or include it directly if you don't mind the long path.

Upvotes: 2

Related Questions