Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:32:02+00:00 2026-06-11T13:32:02+00:00

This is probably a simple mistake. I have a nodejs server running socket.io, I

  • 0

This is probably a simple mistake. I have a nodejs server running socket.io, I have got everything to work within the server.

However, I want to be able to make a CURL post via PHP to the node server, and have it emit the data. I can make the server receive the request, but when I try to emit the data, I get an error saying that the socket is not defined.

This is obvious in my code. My question is, how do I require socket.io before I setup the server? Hopefully a segment my code will help explain my problem:

var http = require('http')
, url = require('url')
, fs = require('fs')
, server;

server = http.createServer(function(req, res) {
// your normal server code
var path = url.parse(req.url).pathname;
switch (path){
   case '/':
     res.writeHead(200, {'Content-Type': 'text/html'});
     res.write('<h1>Hello! <a href="/index.html">Enter</a></h1>');
     res.end();
     break;
   case '/index.html':
     fs.readFile(__dirname + path, function(err, data){
        if (err) return send404(res);
        res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'})
        res.write(data, 'utf8');
        res.end();
     });
    break;
   case '/write.html':
     fs.readFile(__dirname + path, function(err, data){
        if (err) return send404(res);
        res.write(data, 'utf8');
        res.end();
     });
   break;
  case '/post':
     console.log(req.toString());
     socket.broadcast.emit('user', {state: 'PHP Post', userid: 'PHP'});
     res.writeHead(200);
     res.end();
   break;

  default: send404(res);
}
}),

send404 = function(res){
   res.writeHead(404);
   res.write('404');
   res.end();
};

server.listen(843);

// socket.io 
var io = require('socket.io').listen(server);

// Regular socket.io events
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:32:04+00:00Added an answer on June 11, 2026 at 1:32 pm

    how do I require socket.io before I setup the server?

    That part is pretty easy; at the top of your file, do something like:

    var http = require('http')
    , url = require('url')
    , fs = require('fs')
    , socketIO = require('socket.io')  // <-- added line
    , server;
    

    and at the bottom:

    var io = socketIO.listen(server);
    

    The problem is, in your POST handler, you’re using socket.broadcast.emit, but socket isn’t defined. Are you trying to send a message to all Socket.IO users? If so, you can use io.sockets.emit; I’d probably do something like this:

    var http = require('http')
    , url = require('url')
    , fs = require('fs')
    , server
    , io;
    
    ...
    
    case '/post':
       console.log(req.toString());
       io.sockets.emit('user', {state: 'PHP Post', userid: 'PHP'});
       res.writeHead(200);
       res.end();
     break;
    
    ...
    
    io = require('socket.io').listen(server);
    

    If you’re trying to send data to a single socket, or every socket except for a particular one (which is how you’d normally use socket.broadcast), you’ll somehow need to map HTTP requests to Socket.IO sockets; using sessions for this is common.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a simple misunderstanding on my part. Have a simple interface: public
this is probably pretty simple, but I've got this text file containing a bunch
This is probably a simple question, but i have been unable to find a
I'm new to jQuery so this is probably simple, but... If I have this
This is probably an obvious mistake on my part. I have a C# page
This is probably simple but I do not seem to get it to work.
ANSWER: Allright, that was quite simple (probably I made this mistake coz I need
This is probably some simple mistake, I just can't seem to find it in
This is probably simple, I want to replace the _ (underscore) with nothing (space)
this is probably simple but i don't know how to do it. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.