Reputation: 7752
I am using node js, expressjs, socketio, redis with Django.
const PORT = 8008;
const HOST = 'localhost';
var express = require('express'),
http = require('http'),
server = http.createServer(app);
var app = express();
const redis = require('redis');
log('info', 'connected to redis server');
const io = require('socket.io');
if (!module.parent) {
server.listen(PORT, HOST);
const socket = io.listen(server);
socket.on('connection', function(client) {
const subscribe = redis.createClient(6379, '127.0.0.1')
subscribe.subscribe('test');
subscribe.on("message", function(pattern, channel, message) {
client.send(channel, message);
log('msg', "received from channel #" + channel + " : " + message);
});
});
<script src="http://localhost:8008/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect("http://localhost:8008/");
socket.on('connect', function(data){
socket.emit('subscribe', {channel:'test'});
});
socket.on('message', function (data) {
console.log('received a message: ', data);
});
Server is sending message to the channel but when it's loading on the client,I get the following error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8008/socket.io/1/?t=1399898337175. This can be fixed by moving the resource to the same domain or enabling CORS.
Upvotes: 2
Views: 8172
Reputation: 747
You can also configure your socket server to enable wildcard origins.
io.configure('development', function(){
io.set('origins', '*:*');
}
Or
io.set('origins', '*:*');
Check https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO for more
Upvotes: 7
Reputation: 6764
You have to enable cors on server side.
add this in your node code
// Enables CORS
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
};
};
app.configure(function() {
// enable CORS!
app.use(enableCORS);
});
Upvotes: 1